Thanks to EmilStenstrom for raising this, and to Chris Piwoński for all of the fixes and most of the tests.