move allocate out of loop