Add some test cases and fix some bugs