Adding some tests and refactoring the code