I was able to share the write() method, but I don't think I'll be able to merge the loops.