Fix variable scope issue

Resolves #9919.