Fixed variable scoping issue