improved wait loop