Avoid shallow copy by doing deepcopy earlier.