Improved test cases, fixed some bugs