Additional tests and bugfixes

This is a merge of some previous additional tests and bugfixes.