Refactor AI code.