Refactoring code & adding a Max_contacts_per_page & Debugging for SQL