Add tests and fix a few things