tests and small refactoring