moved function declarations and updated code of ai_main