refactor and add JUnit tests