Fixed bugs revealed in the unit test.