improved tests and small refactoring