Added check to prevent multiple calls to agent:set_state