Various fixes

See the commits for details. From my triage queue.