Add wait_for_ajax to prevent race conditions