Reorganized a bit to help make the Lua API easier to write.