remove tsort depedency by implementing sorting ourselves