fix bug and improve test cases