Replace Thread.Sleep to wait and until