Refactor reasoning algorithm.