removed unneeded variables, move some variables to local scope