fixed some variable scope bugs