More refactoring and improvements to AI framework