First pass at generalizing toposort