Upstream changes

The commit history is a bit of a mess, and I can try to fix that if you want, but the actual diff is correct.