Added a few JUnit tests and various fixes