Move insertion tracking outside of loop to avoid redundant execution.