added checkstyle comments
added more efficient topological sort