Changes all writing loops to a generalized function with lambda for serializing the final prv line. This closes #9.