ksh math refactor