Ported bugfixes from 1.7.2 version.