Re-wrote bugfix for task 527 (graph ordering)