reorganized Lua code