Refactored page open code