refact with lua script for higher perf