fixing a memory leak where environments were held when not needed