waitFor function updated