refactor score function