add tests and small refactoring