Merge in changes from upstream.

Not sure what the best way to do this would have been - there's merge conflicts so it needs to be in a branch, but the upstream commits got squashed with this...

At any rate, tested, built, our tests pass.