Improve isPrime() implementation