Fix several bugs. The project can pass all tests now.