Factored out common file code (regardless of I/O)