Refactor math.