minor refactoring, add tests