diff --git a/miscmaths.cc b/miscmaths.cc
index 5aed0ce1cfa5f70d303a429afbb4b92460ff0042..e603e96c0d8f0f6dda81da13ce5da6cae50b11dd 100644
--- a/miscmaths.cc
+++ b/miscmaths.cc
@@ -1560,7 +1560,7 @@ void element_mod_n(Matrix& Mat,double n)
   for( int j=1;j<=Mat.Ncols();j++){
     for( int i=1;i<=Mat.Nrows();i++){
 
-      if( !( (Mat(i,j)>0) && (Mat(i,j)<n) ) ){ 
+      while( !( (Mat(i,j)>0) && (Mat(i,j)<n) ) ){ 
 	tmp = ( Mat(i,j) - rounddouble(Mat(i,j)/n)*n );
 	Mat(i,j)= tmp > 0 ? tmp : tmp + n;
       }