refactoring - move temp variables to innermost scopes to prevent bugs
linting