Re-organize lua scripts.
Refactor: extract error handling.