fixed some variable scoping bugs