Refactor rank and rank_in_group methods