Fix looping and pagination