refining merging code