Fixed bugs in paging code