Improved prime checking