Moved variable declaration outside inner function

Doing this due to request in #980, although I'd prefer to move variables to the inner-most scope instead.