Some refactoring of the AI subsystem.