Adapted for Lua 5.2

The code has been changed avoiding global variable declarations, and making it work with Lua 5.2
Compatibility with Lua 5.1 has not been checked