Hand merge out from upstream with modifications to allow the tests to
pass.  Some tests are disabled, and will have to be looked at in more
detail.