unit testing and a bit of refactoring