lots of changes:
* make CG into an object to avoid leaking state
* small changes to GLM