Refactor PrimeFactors code