A tsort try
Borrowed from Justin's tsort