fix variable scoping