Fixed sorting and added priority