Sam&Michal: Refactored next_page function