I wonder if the cloning should not happen earlier in the function.