Added tests, code refactoring