Copying Simon M's fix for 650 to the new codegen