improved performace of generate_primes