Refactored the refineScores function.