large_ratio <- ( abs(Σ[-d] / Σ[-1]) > 3 )
     K <- if (any(large_ratio)) max(2, which.min(large_ratio)) else d
   }
+  else if (K > d)
+    stop("K: integer >= 2, <= d")
 
   # Step 1: generate a family of d matrices to joint-diagonalize to increase robustness
   d = ncol(X)
 
   if (!is.numeric(Y) || any(is.na(Y)) || any(Y!=0 & Y!=1))
     stop("Y: binary vector with 0 and 1 only")
   link <- match.arg(link)
-  if (!is.numeric(K) || K!=floor(K) || K < 2)
-    stop("K: integer >= 2")
+  if (!is.numeric(K) || K!=floor(K) || K < 2 || K > ncol(X))
+    stop("K: integer >= 2, <= d")
 
   if (is.null(M))
   {