add a unit test and more refactorings