Fix some capture issues in lambda optimization.