Added some tests and refactoring