Add tests. Fix bugs.