Refactor to move some c source generation code into a class