Bugfixes and Tests

There are quite a few tests and bugfixes here.
This is backend stuff which addresses issues #1, #2, #5, #7 and #8