fixed factorize function