Reworked toposort

Should work now