improve the implementation of digits function.