Actually implemented the priority sorting.