added tests and a tidy up