Add tests and fix some initial bugs