lua: Reduce complexity and code duplication in example module and add more documentation.