add testcase and fix many bugs