Bugfixes

Various bugfixes, split off from pull request 738