Bug fixes and JUnit Tests