Adding topological reordering function to reference code