Added tests, refactoring