--- a/eeprom.c
+++ b/eeprom.c
@@ -27,6 +27,7 @@
 
 EEPROMSTATE eepromstate;
 
+#ifdef EEPROM_SUPER_COMPLICATED
 void init_eeprom()
 {
   /* Define FLASH programming time */
@@ -35,30 +36,47 @@
   FLASH_Unlock(FLASH_MEMTYPE_DATA);
     
   eepromstate.pristine = 0; 
-  eepromstate.cur_state = 1; 
   get_last_pcid_eeprom();
   get_last_state_eeprom();
+  eepromstate.pcid++;
+
 }
 
-void write_state_eeprom()
+
+void write_state_eeprom(uint8_t state)
 {
-  uint8_t cur_pcid;
+  uint8_t i;
   
-  cur_pcid = (eepromstate.last_pcid+1); //implies MOD 256  
-  eepromstate.cur_state++;
-  //increase PCID everytime STATE wrap around
-  if(eepromstate.cur_state >= EEPROM_STATES)
+  
+  if(state == 0)
   {
-    eepromstate.cur_state = 0;
-    eepromstate.last_pcid = cur_pcid;
+    eepromstate.pcid++; //implies MOD 256 
+  } else if( state < eepromstate.last_state ) {
+    eepromstate.pcid++; //implies MOD 256
+    eepromstate.last_state = 0;
+    eepromstate.last_addr++; 
+    eepromstate.last_addr %= EEPROM_BYTES;
+    FLASH_ProgramByte(EEPROM_STARTADDR+(eepromstate.last_addr  * EEPROM_GRANULARITY),eepromstate.pcid); 
   }
- 
-  eepromstate.last_addr++; 
-  eepromstate.last_addr %= EEPROM_BYTES;
   
-  FLASH_ProgramByte(EEPROM_STARTADDR+(eepromstate.last_addr  * EEPROM_GRANULARITY),cur_pcid); 
+  if(state == 0)
+  {
+    eepromstate.last_addr++; 
+    eepromstate.last_addr %= EEPROM_BYTES;
+    FLASH_ProgramByte(EEPROM_STARTADDR+(eepromstate.last_addr  * EEPROM_GRANULARITY),eepromstate.pcid); 
+  } else {
+    for( i = 0; i < (state - eepromstate.last_state); i++)
+    {
+      
+      eepromstate.last_addr++; 
+      eepromstate.last_addr %= EEPROM_BYTES;
+      FLASH_ProgramByte(EEPROM_STARTADDR+(eepromstate.last_addr  * EEPROM_GRANULARITY),eepromstate.pcid); 
+    }
+  }
   
+  eepromstate.last_state = state;
 }
+
 //Counts backwards to find the number of 
 uint8_t get_last_state_eeprom()
 {
@@ -83,7 +101,7 @@
       addr = EEPROM_BYTES-1;
     }
     cnt++;
-  } while( cur_byte == eepromstate.last_pcid && cnt <= EEPROM_BYTES);
+  } while( cur_byte == eepromstate.pcid && cnt <= EEPROM_BYTES);
   
   cnt--;
   eepromstate.last_state = cnt % EEPROM_STATES;
@@ -106,7 +124,7 @@
     if( (uint8_t)(cur_byte - last_byte) >= 2 ) // implies MOD 256
     {
       eepromstate.last_addr = addr - 1;
-      eepromstate.last_pcid = last_byte;
+      eepromstate.pcid = last_byte;
       found = 1;
       break;
     }
@@ -120,12 +138,43 @@
     if( (uint8_t)(cur_byte - last_byte ) >= 2 ) // implies MOD 256
     {
       eepromstate.last_addr = EEPROM_BYTES;
-      eepromstate.last_pcid = last_byte;
+      eepromstate.pcid = last_byte;
     } else { // pristine EEPROM condition
       eepromstate.pristine = 1;
       eepromstate.last_addr = 0;
-      eepromstate.last_pcid = 0;
+      eepromstate.pcid = 0;
+      eepromstate.last_state = 0;
     }
   }
 }
+#else
+void init_eeprom()
+{
+  /* Define FLASH programming time */
+  FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD);
+  /* Unlock Data memory */
+  FLASH_Unlock(FLASH_MEMTYPE_DATA);
+  
+  eepromstate.last_state = FLASH_ReadByte(EEPROM_STARTADDR);
+  
+  if (eepromstate.last_state >= EEPROM_STATES)
+  {
+    eepromstate.last_state = 0;
+  }
+}
 
+void write_state_eeprom(uint8_t state)
+{
+
+  eepromstate.last_state = state;
+
+  if(eepromstate.last_state >= EEPROM_STATES)
+  {
+    eepromstate.last_state = 0;
+  } 
+
+  FLASH_ProgramByte(EEPROM_STARTADDR,eepromstate.last_state); 
+
+}
+
+#endif