Complete ai refactoring.