Refactored code to use scopes