Add tests, fix a bug