introduce copy_env function and prepare subsequent stages