fixed the variable scoping