Fixed some bugs, added more test cases