Improve merge logic