Added in JUnit tests, refactored a bit too