Merge pull request #4069 from Zuhayr/master

Bug fixes.
Tentative fix for #4037, unable to reproduce runtime to check.