Looping through data to populate tables