Refactor merging to be independent of parsing