cleanup and refactor by moving page calculation to separate fn