Api for AI and many refactoring.