Using local keyword where appropriate.

For loop control variables in Lua are local by default.
We should also probably employ some naming convention to denote globals.