MISC security fixes

Stuff that I've had for ages locally... and that should probably get in the next release.
Mind the whitespace, please. This file uses tabs.