Adding some tests and refactoring