Moved waiting/ready logic out to the wait loop.