cleanup cg code for loops