basic unit tests, code cleanup and code reorganisation