implement rank function