updated primes method to avoid stack overflows.