Fix scoping issuses with local variables used in lambdas