moved ai code to bottom