Refactoring and added more JUnit tests.