Updated paging logic with modular math suggested by danny.