Add test case, fix bugs