Notice this code and the other below is quite similar. May we can move encapsulate all this inside a `waitBeforeCheck()` function (suggest a better name if you can).