Implemented priority sorting