Updated dependency sorter to actually work correctly in more complicated cases