cleanup cg code
correct prototypes
replace while by for loop