Modified environment logic. Fixes bug 7695