Add tests. Fix bug found by tests.