cleanup cg code
correct a few prototypes
remove some unused macros
replace while loop by for loop