fixed bugs and added test case