moved waiting to loop to wait()