Applied fixes from Mikhail Semenov.