diff --git a/applications/test/codeStream/codeStreamDict1 b/applications/test/codeStream/codeStreamDict1
index 54fb3d0efe9497c15c83617324fc3ac81a8d42d3..ec94ddb17366ae062009bd41da453fa2f0899f07 100644
--- a/applications/test/codeStream/codeStreamDict1
+++ b/applications/test/codeStream/codeStreamDict1
@@ -14,10 +14,15 @@ FoamFile
 }
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
-// #include "codeStreamDefaults"
+// values from outer-scope
+begIter     0;
+endIter     200;
 
 writeInterval   #codeStream
 {
+    // values from inner-scope
+    nDumps      5;
+
     codeInclude
     #{
         #include "fvCFD.H"
@@ -30,14 +35,15 @@ writeInterval   #codeStream
 
     code
     #{
-        scalar start = 0;
-        scalar end = 100;
-        label nDumps = 5;
-        label interval = end - start;
-        Info<<"on-the-fly: " << ((interval)/nDumps) << endl;
-        os  << ((interval)/nDumps);
+        label interval = ($endIter - $begIter);
+        label nDumps = $nDumps;
+        os  << (interval / nDumps);
     #};
 };
 
 
+// play with cleanup
+#remove begIter
+
+
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
diff --git a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C
index 1accc9751789c7306df3509b6c2fc6865fb52ae3..21ffba1f91a4dd2927e619d39a2d69574e281706 100644
--- a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C
+++ b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C
@@ -83,17 +83,21 @@ bool Foam::functionEntries::codeStream::execute
             << exit(FatalIOError);
     }
 
+    // get code dictionary
+    // must reference parent for stringOps::expandDict to work nicely
+    dictionary codeDict("#codeStream", parentDict, is);
+
 
     // Read three sections of code.
     // Remove any leading whitespace - necessary for compilation options,
     // convenience for includes and body.
-    dictionary codeDict(is);
 
     // "codeInclude" is optional
     string codeInclude;
     if (codeDict.found("codeInclude"))
     {
         codeInclude = stringOps::trim(codeDict["codeInclude"]);
+        stringOps::inplaceExpandDict(codeInclude, codeDict);
     }
 
     // "codeOptions" is optional
@@ -101,10 +105,13 @@ bool Foam::functionEntries::codeStream::execute
     if (codeDict.found("codeOptions"))
     {
         codeOptions = stringOps::trim(codeDict["codeOptions"]);
+        stringOps::inplaceExpandDict(codeOptions, codeDict);
     }
 
     // "code" is mandatory
     string code = stringOps::trim(codeDict["code"]);
+    stringOps::inplaceExpandDict(code, codeDict);
+
 
     // Create SHA1 digest from the contents
     SHA1Digest sha;
diff --git a/src/OpenFOAM/db/dynamicLibrary/codeStream/codeStreamTools.C b/src/OpenFOAM/db/dynamicLibrary/codeStream/codeStreamTools.C
index fc1c6f0ffb79b7de0cbcb92044b249b7f6d49f40..aee9c859d87d43f4a22b7537b687aad03e25774e 100644
--- a/src/OpenFOAM/db/dynamicLibrary/codeStream/codeStreamTools.C
+++ b/src/OpenFOAM/db/dynamicLibrary/codeStream/codeStreamTools.C
@@ -159,12 +159,11 @@ void Foam::codeStreamTools::copyAndExpand
     {
         is.getLine(line);
 
-        // normal expansion according to mapping
+        // expand according to mapping
+        // expanding according to env variables might cause too many
+        // surprises
         stringOps::inplaceExpand(line, mapping);
 
-        // expand according to env variables
-        stringOps::inplaceExpandEnv(line, true, true);
-
         os  << line.c_str() << nl;
     }
     while (is.good());
diff --git a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchScalarField.C b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchScalarField.C
index b87a560ac00125d2a12c5222e21d95b26e287781..05c778ad5b4888ab28cfc47c3baeebb77573d24a 100644
--- a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchScalarField.C
+++ b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchScalarField.C
@@ -96,6 +96,7 @@ void Foam::codedFixedValueFvPatchScalarField::writeLibrary
     if (dict.found("codeInclude"))
     {
         codeInclude = stringOps::trim(dict["codeInclude"]);
+        stringOps::inplaceExpandDict(codeInclude, dict);
     }
 
     // "codeOptions" is optional
@@ -103,10 +104,13 @@ void Foam::codedFixedValueFvPatchScalarField::writeLibrary
     if (dict.found("codeOptions"))
     {
         codeOptions = stringOps::trim(dict["codeOptions"]);
+        stringOps::inplaceExpandDict(codeOptions, dict);
     }
 
     // "code" is mandatory
     string code = stringOps::trim(dict["code"]);
+    stringOps::inplaceExpandDict(code, dict);
+
 
     // Create SHA1 digest from the contents
     SHA1Digest sha;