Refactor page edit code