Added tests and refactoring.