Fixing issue with null group_id's and moving table creation/dropping outside of loop for performance improvement.