Rework priority calculation loop