I've simplified an implementation along the lines you suggest, and tested it via ert. I'm attaching the latest version of the patch. Please let me know what you think.