Move duplicate code to openOverlay function