Added tests, small refactoring