Adding factorial and safeguards.