Fixed variable scoping issues