Add tests. Fix a couple small bugs