move connection outside of for loop