Refactor to reduce memory use (write as we parse, not after we parse)