Refactored score function