Add tests and fixes