refactored code to accept number of digits