[TASK] Fix a couple hundred CGL and declaration issues

Please do give this one a proper testing. I've verified the parts involved in unit tests and my local dev setup but there might be hidden stuff b/c of variable assignments being changed (initialised with NULL, intermediate variables).