Refactoring factorial for faster output