Add tests, fix bugs uncovered by tests
Merge pull request #264 from stianjensen/test-2-the-testening

Add tests, fix bugs uncovered by tests