Merged waitsFor into reusable function