This is a repost of a patch which I sent a while back but that never got merged. Can anyone see any problems with it?