(partially) fixed the environment and scoping issues