Add some basic tests
Fix a couple bugs caught by tests