Added test cases, fixed bugs