diff --git a/nand-image-builder.c b/nand-image-builder.c
index e88665885652326a1297b181e0246e801bda6353..6a4a7ca95b24692e8bba2e68646dd05073986ccd 100644
--- a/nand-image-builder.c
+++ b/nand-image-builder.c
@@ -89,7 +89,7 @@ static void swap(uint8_t *first, uint8_t *second, int amount)
 {
     uint8_t temp;
 
-    while(amount--)
+    while (amount--)
     {
         temp = *first;
         *first++ = *second;
@@ -132,7 +132,7 @@ int main(int argc, char **argv)
     uint32_t numBytesRead, totalBytesRead = 0;
     uint32_t block_size, page_size, ecc_level, spare_area, field_order = 0;
     uint32_t sector_sz, oob_sz, oob_ecc_len, oob_ecc_ofs = 0;
-    int pages_in_block, i ,j = 0;
+    int pages_in_block, i, j = 0;
     const char *input_file = NULL;
     const char *output_file = NULL;
     FILE *in_file = NULL, *out_file = NULL;
@@ -247,7 +247,7 @@ int main(int argc, char **argv)
         goto exit;
     }
 
-   /* if ((st.st_size % block_size) != 0)
+    /* if ((st.st_size % block_size) != 0)
     {
         printf("File size not multiple of block size\n");
         goto exit;
@@ -294,7 +294,7 @@ int main(int argc, char **argv)
     /* Initialize BCH lib data and parameters */
 
     struct bch_control *bch = init_bch(field_order, ecc_level, 0);
-	
+
     if (!bch)
         goto exit;
 
@@ -311,14 +311,13 @@ int main(int argc, char **argv)
     printf("OOB_ECC_LEN %d\n", oob_ecc_len);
     printf("OOB_ECC_OFS %d\n", oob_ecc_ofs);
 
-    
-   while (!feof(in_file))
- 
+    while (!feof(in_file))
+
     {
 
         clear_pagebuffer();
 
-        numBytesRead = fread(page_buffer, (size_t)1, (sector_sz ) * SECTORS_PER_PAGE, in_file);
+        numBytesRead = fread(page_buffer, (size_t)1, (sector_sz)*SECTORS_PER_PAGE, in_file);
         totalBytesRead += numBytesRead;
 
         /* printf("\r\nRead %d bytes [%d%%] from file...\r\n",
@@ -331,7 +330,8 @@ int main(int argc, char **argv)
             goto exit;
         }
 
-	if (numBytesRead != (sector_sz ) * SECTORS_PER_PAGE) break; // skip if byte is less than bytes read from fread api.
+        if (numBytesRead != (sector_sz)*SECTORS_PER_PAGE)
+            break; // skip if byte is less than bytes read from fread api.
 
         memset(cmp_buffer, 0, sector_sz * SECTORS_PER_PAGE);
 
@@ -341,31 +341,38 @@ int main(int argc, char **argv)
 		  for(i=0; i < 2048; i++)
 			printf("0x%X\t", cmp_buffer[i]); */
 
-        for (i = 0; i != SECTORS_PER_PAGE; ++i)
+        for (i = 0; i < SECTOR_SZ * SECTORS_PER_PAGE; i += 4)
         {
+            if (*(uint32_t *)(page_buffer + i) != 0xffffffff)
+            {
+
+                for (i = 0; i != SECTORS_PER_PAGE; ++i)
+                {
 
-          
+                    swap(&page_buffer[(i + 1) * sector_sz], &page_buffer[sector_sz * SECTORS_PER_PAGE + (i * oob_sz)], oob_sz);
 
-	 swap(&page_buffer[(i + 1) * sector_sz], &page_buffer[sector_sz * SECTORS_PER_PAGE + (i * oob_sz)], oob_sz);
+                    memset(ecc, 0, OOB_ECC_LEN);
 
-		memset(ecc, 0, OOB_ECC_LEN);
+                    encode_bch(bch, &page_buffer[i * sector_sz], sector_sz + oob_ecc_ofs, ecc);
 
-            encode_bch(bch, &page_buffer[i * sector_sz] , sector_sz + oob_ecc_ofs, ecc);
-					
+                    swap(&page_buffer[(i + 1) * sector_sz], &page_buffer[sector_sz * SECTORS_PER_PAGE + (i * oob_sz)], oob_sz);
 
-            
-		swap(&page_buffer[(i + 1) * sector_sz], &page_buffer[sector_sz * SECTORS_PER_PAGE + (i * oob_sz)], oob_sz);
-		
+                    for (j = 0; j < oob_ecc_len; j++)
+                    {
+                        page_buffer[sector_sz * SECTORS_PER_PAGE + ((i + 1) * oob_sz) - oob_ecc_len + j] = ecc[j];
+                    }
 
-		for(j = 0; j < oob_ecc_len; j++)
-                        {
-                            page_buffer[sector_sz * SECTORS_PER_PAGE + ((i + 1) * oob_sz) - oob_ecc_len + j] = ecc[j];
-                        }
-		
+                    //page_buffer[sector_sz * SECTORS_PER_PAGE + ((i + 1) * oob_sz) - oob_ecc_len +] |= ecc[oob_ecc_len - 1] >> 4;
+                    //fwrite(page_buffer, (sector_sz + oob_sz) * SECTORS_PER_PAGE, 1, out_file);
+                    //goto exit;
+                    //sector_oob[oob_ecc_ofs + oob_ecc_len - 1] |= ecc[oob_ecc_len - 1] >> 4;
+                }
+            }
+            break;
         }
 
         fwrite(page_buffer, (sector_sz + oob_sz) * SECTORS_PER_PAGE, 1, out_file);
-	
+
         i = memcmp(cmp_buffer, page_buffer, (sector_sz)*SECTORS_PER_PAGE); // compare the page buffer with cmp_buffer to verify page has not modified except oob spare size.
 
         if (i != 0)
@@ -379,10 +386,9 @@ int main(int argc, char **argv)
 
         if (!(count & countMask))
         {
-           /* printf("block No  %d\r\n", blocks_num); */
+            /* printf("block No  %d\r\n", blocks_num); */
             blocks_num++;
         }
-
     }
 
 exit: