Starting to translate Python implementation to Lua.