I've attached a new version of the patch incorporating the latest discussions.