Fixed some bugs and test cases