Fix bug that prevented globally defined environment variables
from being overridden by local definitions. The diffs here are
a little large because the most sensible way to do this was to
switch to using dictionaries for building the environment vars
(to make replacing values easy), and then convert them to a
VarArray later in a format that can be used as an environ
pointer.