topological sort - still not fully checked