Refactored code for page transitions