Updated to do looping until empty vs calculating number of pages