Add JUnit4 tests, and fix bugs