Refactor factorial