Added unit tests and some minor tidy up