Rewrote / refactored the paging function (again).