added option to write to file serially so that memory is not overused