Environment fix 3

Last version of the fix for the issue described in #1059, #1060 and #1071. This time we've got a fix that is both simple, correct and minimally intrusive. @Antique, if you can try those before we push it to next, I'd appreciate it, thanks!