Fix some bugs, add more test cases