Fix bugs to pass all test cases