Wrote (and tested) topolgical sort.