fix primes and introduce freeze check in bfs