Add while loop to wait() to guard against "spurious wakeups"