Copy and pasted code, from sortingmachinery.dm.