Bugfixes

See commit for details.