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.

Fixes #80: Sync with upstream [07f8c2a069a96e8cd3212a037910baf76983f409]