Add tests and bugfix.