You could move this to earlier in the loop (line 208)