lua ported from python version