refactored ranking function