Make a copy of the environment

Without making a copy, the environment will be tainted for subsequent
actions.
Merge pull request #3891 from rca/make_copy_of_environment

Make a copy of the environment