Add some tests, fix some bugs