fix variable scoping issue