better generalized I/O routine