Fixed bug for 'for' loop and removed redeclaration of variables