Adding some tests and small refactoring