Fixed variable scope, plus suggested changes