Refactor factorize()