added tests and code refactor