Simplify by moving variables out of function scope