Refactor to separate parsing and writing