Sync with upstream patches.

Synced all interesting and usable upstream patches and fixed
any merge conflicts and changes needed to fit in the new world.

Synced up to Branch-5.2 - git commit
6decfe0792db538aa18cb07b2d06aba075e08bf5

Fixes #47: Sync with upstream [6decfe0792db538aa18cb07b2d06aba075e08bf5]