Fixing bug in topological sort