refactored to check only against previous primes