Various bug fixes

All tested, of course.