move file read up to main loop
simplify file write loop