fix some bugs, add some test cases