move the malloc into the outer loop