Adding initial ai logic.
Refactoring some promises. Fixing tests for AI.
Refactoring AI and fixing tests.