Minor bugfixes

Please review, new tests included, all tests pass