Added priority sorting before applying