refactor math