redmine5411 crash fix

A bit of tidy-up and a warning fix along the way, too.