Backport all but tests to support Ruby 1.8.7