Fix bug in wait()