added topological sorting of categories - first attempt