Add tests. Fix bugs