reorganized helper funcktion for ranking