Fixing a performance regression from way back in de89f7c.
Also rewriting most of my changes to account for that.