Add more tests and fixes a few bugs