added custom wait function that relies on jQuery deferred