Fix merge algorithm