Bugfixes

This branch contains a series of bugfixes.  You can merge all of them or pick some of them as you want.