Some refactoring in the AI code