Master cherry pick

Cherry pick some old unmerged changes. I don't know enough about the library to tell whether this is actually a good idea. If not, it would be better to revert these changes in the develop branch.

If this is accepted then everything up to e9b92c58c887837cb8b20653bb3947a1cacbee57 will have been merged, so that can be used as a merge base.