diff --git a/tracy/tools/max4.cc b/tracy/tools/max4.cc
index a95c17be725379b0227bab75eb56a2641f1a159b..da8bc02a440f555e0cdaed93a4f6c52a6139e9de 100644
--- a/tracy/tools/max4.cc
+++ b/tracy/tools/max4.cc
@@ -1,12 +1,10 @@
 #define ORDER 1          
 
-#include "tracy_lib.h"
-
 int no_tps = ORDER; // arbitrary TPSA order is defined locally
 
 extern bool  freq_map;
 
-
+#include "tracy_lib.h"
 
 //***************************************************************************************
 //
@@ -15,7 +13,6 @@ extern bool  freq_map;
 //****************************************************************************************
  int main(int argc, char *argv[])
 {
-
   const long  seed = 1121;
   const bool All = TRUE;
   iniranf(seed); setrancut(2.0);
@@ -24,12 +21,13 @@ extern bool  freq_map;
   // IDs accounted too if: wiggler model and symplectic integrator (method = 1)
   globval.H_exact     = false; 
   globval.quad_fringe = false;                // quadrupole fringe field
-  globval.Cavity_on   = false;                 // include RF cavity 
+  
   globval.radiation   = false;                // synchrotron radiation
   globval.emittance   = false;                 // emittance
   globval.pathlength  = false; 
-  globval.bpm         = 0;
+
   
+ 
   // overview, on energy: 25-15
   //const double  x_max_FMA = 20e-3, y_max_FMA = 10e-3; //const x_max_FMA = 25e-3, y_max_FMA = 15e-3;
   //const int     n_x = 80, n_y = 80, n_tr = 2048;
@@ -44,78 +42,73 @@ extern bool  freq_map;
   //const double  x_max_FMA = 3e-3, delta_FMA = 7e-2;
   //const int     n_x = 28, n_dp = 56, n_tr = 2048;
   
-  double nux = 0.0 , nuz = 0.0, ksix = 0.0, ksiz =  0.0;
+  double nux=0.0, nuz=0.0, ksix=0.0, ksiz=0.0;
+  
   bool chroma;
   double dP = 0.0;
   long lastpos = -1L;
   char str1[S_SIZE];
-  if (true)
-    Read_Lattice("/home/nadolski/codes/tracy/maille/soleil/solamor2_tracy3"); 
-  //  Read_Lattice(argv[1]); //sets some globval params
-  else
-    rdmfile("flat_file.dat"); //instead of reading lattice file, get data from flat file
-    
-    
-  globval.quad_fringe = true;                // include quadrupole fringe field
-  /*globval.bpm = ElemIndex("bpm");  //definition for max4 lattice, bpm                  
-  globval.hcorr = ElemIndex("ch"); 
-  globval.vcorr = ElemIndex("cv");*/   
+  
+  /************************************************************************
+      start read in files and flags
+  *************************************************************************/
+  read_script(argv[1], true);
 
+
+ /************************************************************************
+    end  read in files and flags
+  *************************************************************************/
+    
+  
   
-  //no_sxt(); //turns off sextupoles
   
-  Ring_GetTwiss(true, 0.0); 
-  printglob(); //gettwiss computes one-turn matrix arg=(w or w/o chromat, dp/p)
-  get_matching_params_scl();
-  get_alphac2(); // compute up to 3rd order mcf
-  GetEmittance(ElemIndex("cav"), true);
+  
+  
+//  if (true)
+//  //  Read_Lattice("/home/nadolski/codes/tracy/maille/soleil/solamor2_tracy3"); 
+//    Read_Lattice(argv[1]); //sets some globval params
+//  else
+//    rdmfile("flat_file.dat"); //instead of reading lattice file, get data from flat file 
+
+  //no_sxt(); //turns off sextupoles
+ // Ring_GetTwiss(true, 0e-2); //gettwiss computes one-turn matrix arg=(w or w/o chromat, dp/p) 
+  //get_matching_params_scl();
+  //get_alphac2();
+  //GetEmittance(ElemIndex("cav"), true);
+  
+//prt_lat("linlat.out", globval.bpm, true);  /* print lattice file for nsrl-ii*/
+prtmfile("flat_file.dat"); // writes flat file   /* very important file for debug*/
+//prt_chrom_lat(); //writes chromatic functions into chromlat.out
+//  printlatt();  /* print out lattice functions */
+ 
 
 /* print lattice file */
-  prt_lat("linlatBNL.out", globval.bpm, All); // BNL print for all elements
+//  prt_lat("linlatBNL.out", globval.bpm, All); // BNL print for all elements
   printlatt();  /* SOLEIL print out lattice functions */
+  printglob();
   
-  prtmfile("flat_file.dat"); // writes flat file
-  prt_chrom_lat(); //writes chromatic functions into chromlat.out
-
   
-  // Using tracking to get tunes and chromaticities
-  if (false) {
-    GetTuneTrac(1026L, 0.0, &nux, &nuz);
-    fprintf(stdout,"From tracking: nux = % f nuz = % f \n",nux,nuz);
-    GetChromTrac(2L, 1026L, 1e-5, &ksix, &ksiz);
-    fprintf(stdout,"From tracking: ksix= % f ksiz= % f \n",ksix,ksiz);
-  }
   
-
     // Flag factory
-  bool TuneTracFlag = true, ChromTracFlag = true;
-  bool FmapFlag = false, ExperimentFMAFlag = false, DetailedFMAFlag = false;
-  bool TuneShiftFlag = false;
-  bool ErrorCouplingFlag = false;  bool CouplingFlag = false;
-  bool MomentumAccFlag = false;
-  bool MultipoleFlag = false;
-  bool FitTuneFlag  = false; double targetnux = 18.202, targetnuz = 10.317;
-  bool FitChromFlag = false; double targetksix = 2.0, targetksiz = 2.6;
-  bool ChamberFlag = false, U20ChamberFlag = false;
-  bool GirderErrorFlag = false;
-  bool SigmaFlag = false;
-  bool PX2Flag = false;
-  bool InducedAmplitudeFlag = false;
-  bool CodeComparaisonFlag = false;
-  bool EtaFlag = false;
-
-  //*************************************************************
-  //=============================================================
+//  bool TuneTracFlag = true; 
+//  bool ChromTracFlag = true;
   
   
-//    // Chamber factory
-//   if (ChamberFlag == false)
-//     ChamberOff(); // no chamber limitations
-//   else if (U20ChamberFlag == true)
-//     DefineChNoU20;
-  PrintCh();
-  LoadApers("Apertures.dat", 1.0, 1.0);
+  //*************************************************************
+  //=============================================================
 
+    
+    // Chamber factory
+  if (ChamberFlag == false)
+     ChamberOff(); // turn off vacuum chamber setting, use the default one
+  else if (ChamberNoU20Flag == true)
+     DefineChNoU20();  // using vacuum chamber setting but without undulator U20
+  else if (ReadChamberFlag == true)
+     ReadCh("Apertures.dat"); /* read vacuum chamber from a file "Apertures.dat" , soleil version*/
+//LoadApers("Apertures.dat", 1.0, 1.0);  /* read vacuum chamber definition for bnl */
+  PrintCh();
+ 
+ 
   // compute tunes by tracking (should be the same as by DA)
   if (TuneTracFlag == true) {
     GetTuneTrac(1026L, 0.0, &nux, &nuz);
@@ -124,7 +117,7 @@ extern bool  freq_map;
 
   // compute chromaticities by tracking (should be the same as by DA)
   if (ChromTracFlag == true){
-    GetChromTrac(2, 1026L, 1e-5, &ksix, &ksiz);
+    GetChromTrac(2L, 1026L, 1e-5, &ksix, &ksiz);
     fprintf(stdout,"From tracking: ksix= % f ksiz= % f \n",ksix,ksiz);
   }
 
@@ -145,32 +138,43 @@ extern bool  freq_map;
 
     //SetKLpar(ElemIndex("QT"), 1, 2L,   0.001026770838382);
   
-//   // coupling calculation
-//   if (CouplingFlag == true){
-//     Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
-//     printlatt();                      /* dump linear lattice functions into "linlat.dat" */
-//     Coupling_Edwards_Teng();
-//     printglob();   /* print parameter list */
-//   }
+  // coupling calculation
+   if (CouplingFlag == true){
+     Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+     printlatt();                      /* dump linear lattice functions into "linlat.dat" */
+  //   Coupling_Edwards_Teng();
+     printglob();   /* print parameter list */
+   }
 
   // add coupling by random rotating of the quadrupoles
-//  if (ErrorCouplingFlag == true){
-//    SetErr();
-//    Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
-//    printlatt();                      /* dump linear lattice functions into "linlat.dat" */
+  if (ErrorCouplingFlag == true){
+    SetErr();
+    Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+    printlatt();                      /* dump linear lattice functions into "linlat.dat" */
 //    Coupling_Edwards_Teng();
-//    printglob();   /* print parameter list */
-//  }
+    printglob();   /* print parameter list */
+  }
 
   // WARNING Fit tunes and chromaticities before applying errors !!!!
   //set multipoles in all magnets
-  if (MultipoleFlag == true){
-    fprintf(stdout, "\n Setting Multipoles \n");
-    //Multipole_new(); /* for thick sextupoles */
-    Multipole(); /* for thin sextupoles */
-    Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
-    printglob();                      /* print parameter list */
+  if (MultipoleFlag == true ){
+    if (ThinsextFlag ==true){
+      fprintf(stdout, "\n Setting Multipoles for lattice with thin sextupoles \n");
+      Multipole_thinsext();  /* for thin sextupoles */
+      
+      Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+      printglob(); 
+     }
+    else{
+      fprintf(stdout, "\n Setting Multipoles for lattice with thick sextupoles \n");
+      Multipole_thicksext();  /* for thick sextupoles */
+      
+      Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
+      printglob(); 
+    }
   }
+                       /* print parameter list */
+  
 
   // PX2 chicane
 //  if (PX2Flag ==true){
@@ -205,7 +209,9 @@ extern bool  freq_map;
   }
 
   if (MomentumAccFlag == true){
-    MomentumAcceptance(1L, 108L, 0.01, 0.05, 100L, -0.01, -0.05, 100L);
+    //MomentumAcceptance(10L, 28L, 0.01, 0.05, 4L, -0.01, -0.05, 4L);
+     MomentumAcceptance(1L, 28L, 0.01, 0.05, 40L, -0.01, -0.05, 40L);
+  //  MomentumAcceptance(1L, 108L, 0.01, 0.05, 100L, -0.01, -0.05, 100L);
   }
 
   // computes Tuneshift with amplitudes
@@ -233,7 +239,7 @@ extern bool  freq_map;
   
  if (EtaFlag == true){
   // compute cod and twiss parameters for different energy offsets    
-    for (double ii=0; ii<=40; ii++) { 
+    for (int ii=0; ii<=40; ii++) { 
     dP = -0.02+ 0.001*ii;
     Ring_GetTwiss(chroma=false, dP);   /* Compute and get Twiss parameters */
     printlatt();                      /* dump linear lattice functions into "linlat.dat" */
@@ -248,27 +254,7 @@ extern bool  freq_map;
     }
 }    
   
-  return 0;
-  
-  
-  
-  //---------------------------------------------------------
-  // test region
-  
- //  trace = true;
- // Get_Disp_dp();
- 
-  //InducedAmplitude(20);
-  
- // Hcofonction(2, 0.001);
-  
-// fmapdp(20L, 21L, 1026L, 25e-3, 0.06, 0.3e-3, true);
- // return 1;
- 
-  //--------------------------------------------------------
-  
-  
-  
+
   
   //*********************************************************************************
   //---------------------------------------------------------------------------------------------------------------------------------