Refactored more stuff into testlib, cleaned up the tests.