Further optimization for primality tests