Add tests and minor fixes