Added test cases, fixed some bugs