Adding pagination code

Hope this doesnt break anything yet.