Added testcases and removed some bugs.