This isn't exactly a patch -- rather, it's a new file followed by a patch. Anyhow I've incorporated most of Kevin Ryde's suggestions.