refactored factorialization method