Hello Sergei!

Apparently, while I was making the changes to this fix, I've also ended up in a small THD refactoring.
Please see commits a1df5fdc and 4e2deb34 as well.

The new version of the fix is 5229ea56. Many changes there, see the commit message.

Regards,
Nikita