Fix to the rank function