corrected variable scoping