Move some of the file handling code to generator