fix scopes for functions defined within other functions

Nested functions now work properly, whereas before the scopes were
messed up.

Only tested for a function nested within another one (just two
levels)