fix variable scoping issues