optimizing logic in loop (moving code around), added pointer array to reduce repetative referencing