Fixing replaceEnvironment + minor refactoring