Add tests and some fixes