refined the merge method to deal with nil values