Why are you looping through groups, then layers in the group, instead of just looping through layers (i.e. use LAYER_LOOP, not your own for loop then a GROUP_LOOP).
Also, it appears that the nested for loop (the one using i) is the same thing as the GROUP_LOOP, so you are unnecessarily looping over every layer twice.
Why are you looping through groups, then layers in the group, instead of just looping through layers (i.e. use LAYER_LOOP, not your own for loop then a GROUP_LOOP).
Also, it appears that the nested for loop (the one using i) is the same thing as the GROUP_LOOP, so you are unnecessarily looping over every layer twice.