Fixed some variable names and scope