moved some code on the environment to be more modular