Cleaned up and refactored AI