Generalize implementation of CG variants