Added tests and refactoring