Fixing bugs found by the JUnit tests