Add more tests fix some bugs