Added test cases and fixed bugs.