Adding proper waiting so no need for threadDelay in tests.