first round refactoring of mathify funciton