fix and refactor lua