improve list merging code