Correct the script in order to wait for the popup close before exit.