Bug fixes for Lua subsystem and refactoring.