Restructured AI script. Added states.