Fix variable scope issue

Resolves #9919.
Merge pull request #9993 from terminalmage/issue9919

Fix variable scope issue