refactoring, tidying up, adding unit tests