Fix rank function. 

Check #34. I'm not sure what the best way