Move some tgen logic to tgen.pyx