Refactor extra math function out of LUA source