Switched to visibilitychanged event, and common function