Fixed bug in the ordering code