Fixed bug in merging, which would allow the first and the last to merge.