improved wait routine