refactor merge logic