lua backend: refactoring and fixing.