basic unit tests, code cleanup and reorganisation