Implemented topological sort - fixed bugs that existed that were exposed by top sort.