Revert "Revert bdb5729..04721ba"

This reverts commit 70b343b30a445b68594528a741391fa064ac739a.
Revert "Reintegrated fixes from 67a8a8c"

This reverts commit d6df0c90c342b38535d78bdc9bf06f639e693c61.