Move optimization logic back into Enumerable