implemented wait to collapse as suggested on stackoverflow