Fix infinite loop in the case of a tie.