topological sorting

+ implemented the algorithm
+ not used anywhere yet
+ code could do with some tidying