Restore some of the tests and fix a bunch of bugs