Refactored merge sort implementation