Fixed copyIfNeeded to do correct things.