Simplify logic in merge sort and remove duplicated code.