Fix some bugs, add auto-generated test cases