Refactoring, added JUnit tests.