Tidy up the tests and clean up code