Refactor evaluate() by breaking into more functions