Reorganize core code into proper Lua modules
The current code evolved organically without strict use of Lua modules. Strict module usage has the form foo = require("foo")
where this statement does not introduce any new globals.
Moving to more strict Lua module usage will require reorganizing some of the core code, giving a more clear dependency picture.