Wrote logic to deal with multiple pages