Added unit tests and tidied code.