refactor digits function