--- a/struct_ls/pfmg3_setup_rap.c	Thu Jun 09 15:56:18 2016
+++ b/struct_ls/pfmg3_setup_rap.c	Thu Dec 22 17:45:20 2016
@@ -19,13 +19,28 @@
  * allow for coarsening to be done in the x- and y-directions also.
  *--------------------------------------------------------------------------*/
 
-#define MapIndex(in_index, cdir, out_index)                     \
-   hypre_IndexD(out_index, cdir) = hypre_IndexD(in_index, 2);   \
-   cdir = (cdir + 1) % 3;                                       \
-   hypre_IndexD(out_index, cdir) = hypre_IndexD(in_index, 0);   \
-   cdir = (cdir + 1) % 3;                                       \
-   hypre_IndexD(out_index, cdir) = hypre_IndexD(in_index, 1);   \
-   cdir = (cdir + 1) % 3;
+//#define MapIndex(in_index, cdir, out_index)                     \
+//   hypre_IndexD(out_index, cdir) = hypre_IndexD(in_index, 2);   \
+//   cdir = (cdir + 1) % 3;                                       \
+//   hypre_IndexD(out_index, cdir) = hypre_IndexD(in_index, 0);   \
+//   cdir = (cdir + 1) % 3;                                       \
+//   hypre_IndexD(out_index, cdir) = hypre_IndexD(in_index, 1);   \
+//   cdir = (cdir + 1) % 3;
+
+template<typename T, typename U, typename V>
+void MapIndex(T& in_index, U& cdir, V& out_index)
+{
+	if(cdir > 3) cdir = cdir - (cdir / 3) * 3;
+	hypre_IndexD(out_index, cdir) = hypre_IndexD(in_index, 2);
+	if(cdir == 3) cdir = 1;
+	else ++cdir;
+	hypre_IndexD(out_index, cdir) = hypre_IndexD(in_index, 0);
+	if(cdir == 3) cdir = 1;
+	else ++cdir;
+	hypre_IndexD(out_index, cdir) = hypre_IndexD(in_index, 1);
+	if(cdir == 3) cdir = 1;
+	else ++cdir;
+}
  
 /*--------------------------------------------------------------------------
  *  Sets up new coarse grid operator stucture.
