Started writing pagination algorithm