refactor average_rating function