refactored function scope