diff --git a/rubix_postproc.sh b/rubix_postproc.sh
index 75ea19a638f854f346f9fa228c9bc7799fbfd998..e7785705a786de6d2855d2f420e2b4eeedd0bf42 100644
--- a/rubix_postproc.sh
+++ b/rubix_postproc.sh
@@ -1,9 +1,10 @@
 #!/bin/sh
 
-out_dir=$1
-dataLR=$2
-maskLR=$3
-dataHR=$4
+subjdir=$1
+filterflag=$2
+
+out_dir=${subjdir}.rubiX
+maskLR=nodif_brain_maskLR
 
 
 numfib=`${FSLDIR}/bin/imglob ${out_dir}/diff_slices/data_slice_0000/f*samples* | wc -w | awk '{print $1}'`
@@ -84,9 +85,15 @@ if [ `imtest ${out_dir}/merged_th1samples` -eq 1 ];then
   if [ `imtest ${out_dir}/merged_ph1samples` -eq 1 ];then
     if [ `imtest ${out_dir}/merged_f1samples` -eq 1 ];then
       rm -rf ${out_dir}/diff_slices
-      rm -f ${out_dir}/${dataLR}_slice_*
-      rm -f ${out_dir}/${dataHR}_newslice_* 
-      rm -f ${out_dir}/${maskLR}_slice_*
+      rm -f ${out_dir}/dataLR_slice_*
+      rm -f ${out_dir}/dataHR_newslice_* 
+      rm -f ${out_dir}/maskLR_slice_*
+      if [ `imtest ${out_dir}/grad_devLR_slice_0000` -eq 1 ];then
+	  rm -f ${out_dir}/grad_devLR_slice_*
+      fi	  
+      if [ `imtest ${out_dir}/grad_devHR_newslice_0000` -eq 1 ];then
+	  rm -f ${out_dir}/grad_devHR_newslice_*
+      fi	  
     fi
   fi
 fi
diff --git a/rubix_preproc.sh b/rubix_preproc.sh
index fe23981f66c9191c7d2ba8a2c1d30da4a86f8d7b..85c55b6ba8522a4a22826d61eccb2a29c6d4ccb2 100644
--- a/rubix_preproc.sh
+++ b/rubix_preproc.sh
@@ -1,18 +1,51 @@
 #!/bin/sh
 
-dataLR=$1
-maskLR=$2
-dataHR=$3
-out_dir=$4
+subjdir=$1
+filterflag=$2
+gflag=$3
 
-${FSLDIR}/bin/fslslice $dataLR $out_dir/$dataLR
-${FSLDIR}/bin/fslslice $maskLR $out_dir/$maskLR
-${FSLDIR}/bin/fslslice $dataHR $out_dir/$dataHR
+out_dir=${subjdir}.rubiX
+maskLR=nodif_brain_maskLR
 
-pixdim3LR=`$FSLDIR/bin/fslval $dataLR pixdim3`
-pixdim3HR=`$FSLDIR/bin/fslval $dataHR pixdim3`
-nslicesLR=`$FSLDIR/bin/fslval $dataLR dim3`
-nslicesHR=`$FSLDIR/bin/fslval $dataHR dim3`
+if [ ${filterflag} -eq 1 ]; then
+    cp ${subjdir}/bvals $out_dir/bvalsLR
+    cp ${subjdir}/bvecs $out_dir/bvecsLR
+    cp ${subjdir}/bvecs $out_dir/bvecsHR
+    cp ${subjdir}/bvals $out_dir/bvalsHR
+    ${FSLDIR}/bin/fslslice $subjdir/data $out_dir/$dataHR
+    HighRes=`fslval ${subjdir}/data pixdim1`
+    LowRes=`echo "${HighRes} * 2" | bc -l`
+    echo "Create Downsampled version of data at ${LowRes} mm isotropic"
+    flirt -in ${subjdir}/nodif_brain_mask -ref ${subjdir}/nodif_brain_mask -applyisoxfm ${LowRes} -interp nearestneighbour -out ${out_dir}/nodif_brain_maskLR
+    flirt -in ${subjdir}/data -ref ${subjdir}/data -applyisoxfm ${LowRes} -interp sinc -out ${out_dir}/dataLR
+    if [ ${gflag} -eq 1 ]; then
+	echo "Create Downsampled version of grad_dev at ${LowRes} mm isotropic"
+	flirt -in ${subjdir}/grad_dev -ref ${subjdir}/grad_dev -applyisoxfm ${LowRes} -interp nearestneighbour -out ${out_dir}/grad_devLR
+	${FSLDIR}/bin/fslslice $subjdir/grad_dev $out_dir/grad_devHR
+	${FSLDIR}/bin/fslslice ${out_dir}/grad_devLR $out_dir/grad_devLR
+    fi
+    dataHRname=${subjdir}/data
+    dataLRname=${out_dir}/dataLR
+else
+    cp ${subjdir}/bvalsLR $out_dir/bvalsLR
+    cp ${subjdir}/bvecsLR $out_dir/bvecsLR
+    cp ${subjdir}/bvecsHR $out_dir/bvecsHR
+    cp ${subjdir}/bvalsHR $out_dir/bvalsHR
+    ${FSLDIR}/bin/fslslice $subjdir/dataLR $out_dir/$dataLR
+    ${FSLDIR}/bin/fslslice $subjdir/$maskLR $out_dir/$maskLR
+    ${FSLDIR}/bin/fslslice $subjdir/dataHR $out_dir/$dataHR
+    if [ ${gflag} -eq 1 ]; then
+	${FSLDIR}/bin/fslslice $subjdir/grad_devLR $out_dir/grad_devLR
+	${FSLDIR}/bin/fslslice $subjdir/grad_devHR $out_dir/grad_devHR
+    fi
+    dataLRname=${subjdir}/dataLR
+    dataHRname=${subjdir}/dataHR
+fi
+
+pixdim3LR=`$FSLDIR/bin/fslval ${dataLRname} pixdim3`
+pixdim3HR=`$FSLDIR/bin/fslval ${dataHRname} pixdim3`
+nslicesLR=`$FSLDIR/bin/fslval ${dataLRname} dim3`
+nslicesHR=`$FSLDIR/bin/fslval ${dataHRname} dim3`
 zratio=`echo "scale=0; $pixdim3LR / $pixdim3HR" | bc -l`
 
 slice=0
@@ -25,14 +58,22 @@ do
     do 
 	HRslice=`echo "( ( $slice + 1 ) * $zratio ) - $count" | bc -l`
 	HRslicezp=`$FSLDIR/bin/zeropad $HRslice 4`
-	filenames[arr_cnt]=${out_dir}/${dataHR}_slice_${HRslicezp}
+	filenames[arr_cnt]=${out_dir}/dataHR_slice_${HRslicezp}
+	gfilenames[arr_cnt]=${out_dir}/grad_devHR_slice_${HRslicezp}
 	count=$(($count - 1))
 	arr_cnt=$(($arr_cnt + 1))
     done
-    fslmerge -z ${out_dir}/${dataHR}_newslice_${slicezp} `echo ${filenames[*]}` 
+    fslmerge -z ${out_dir}/dataHR_newslice_${slicezp} `echo ${filenames[*]}` 
+    if [ ${gflag} -eq 1 ]; then
+	fslmerge -z ${out_dir}/grad_devHR_newslice_${slicezp} `echo ${gfilenames[*]}` 
+    fi
     slice=$(($slice + 1))
 done
 
-rm -f ${out_dir}/${dataHR}_slice_*
-
+rm -f ${out_dir}/dataHR_slice_*
+#rm -f ${out_dir}/dataLR
+if [ ${gflag} -eq 1 ]; then
+    rm -f ${out_dir}/grad_devHR_slice_*
+#   rm -f ${out_dir}/grad_devLR
+fi
 echo Done