fix bugs; adapt for test case