fixed a devious bug related to function scope