Moved g2d outside of loop to avoid creating a new object every iteration