Added test cases and fixed some bugs.