Refactoring and adding in AI