Fixed several small bugs. Now all test cases are passed successfully.