fixed variable scoping issues