Revamp setup_env, split some stuff in functions