Rework algorithm to make it correct under 2.6