refactored code for page function