Refactors to use io.WriteCloser correctly