factorize function