Writing tests

Some more tests, and bugfixes raised by testing