Fixed waitsFor function