fixed and optimized is_prime check