minor optimization for factorial function