fixes rank logic and sorts by rank