Added some test cases, fixed some bugs.