minor refactoring and unit tests