Fixed some minor bugs to pass all the unit test cases.