Fix scope issues with function vs variable binding

It is currently possible that variable names chosen for some inner
binding shadow the names of the variables bound by outer binding
constructs. We either might want to disallow that, or at least have
(possibly optional) compiler warnings. This will be addressed as a
separate issue later, though.

This changeset fixes issues with bindings from an outer scope being
incorrectly preferred to bindings from an inner scope. It makes sure
that variable references behave correctly in the sense of lexical
scoping.