Close the popup window only after the callbacks are done