}
                
                CmdLFRead("s");
-               getSamples("8201",true); //capture enough to get 2 complete preambles (4096*2+9)        
+               //getSamples("8201",true); //capture enough to get 2 complete preambles (4096*2+9)      
+               getSamples("6144",true);
        } while (!CmdEM410xRead(""));
 
        return 0;
 {
        // loops if the captured ID was in XL-format.
        CmdEM410xWatch(Cmd);
-       PrintAndLog("# Replaying captured ID: %llu", g_em410xid);
+       PrintAndLog("# Replaying captured ID: %" PRIu64 , g_em410xid);
        CmdLFaskSim("");
        return 0;
 }
 }
 
 #define EM_PREAMBLE_LEN 6
-// download samples from device
-// and copy them to Graphbuffer
+// download samples from device and copy to Graphbuffer
 bool downloadSamplesEM(){
        
        // 8 bit preamble + 32 bit word response (max clock (128) * 40bits = 5120 samples)
        setGraphBuf(got, sizeof(got));
        return TRUE;
 }
-//search for given preamble in given BitStream and return success=1 or fail=0 and startIndex
+
+// em_demod 
 bool doPreambleSearch(size_t *startIdx){
        
        // sanity check
                if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305 demodbuffer too small");
                return FALSE;
        }
-       
-       // skip first two 0 bits as they might have been missed in the demod 
-       uint8_t preamble[EM_PREAMBLE_LEN] = {0,0,1,0,1,0};
-       
-       // set size to 15 to only test first 4 positions for the preamble
-       size_t size = (15 > DemodBufferLen) ? DemodBufferLen : 15;
+
+       // set size to 20 to only test first 14 positions for the preamble
+       size_t size = (20 > DemodBufferLen) ? DemodBufferLen : 20;
        *startIdx = 0; 
-       uint8_t found = 0;
-       
-       // em only sends preamble once, so look for it once in the first x bits
-       for (int idx = 0; idx < size - EM_PREAMBLE_LEN; idx++){
-               if (memcmp(DemodBuffer+idx, preamble, EM_PREAMBLE_LEN) == 0){
-                       //first index found
-                       *startIdx = idx;
-                       found = 1;
-                       break;
-               }
-       }
+       // skip first two 0 bits as they might have been missed in the demod
+       uint8_t preamble[EM_PREAMBLE_LEN] = {0,0,1,0,1,0};
        
-       if ( !found) {
+       if ( !preambleSearchEx(DemodBuffer, preamble, EM_PREAMBLE_LEN, &size, startIdx, TRUE)) {
                if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305 preamble not found :: %d", *startIdx);
                return FALSE;
        } 
 
        //test for even parity bits.
        uint8_t parity[45] = {0};
-       memcpy( parity, 45, DemodBuffer);
+       memcpy( parity, DemodBuffer, 45);
        if (!EMwordparitytest(parity) ){
                PrintAndLog("DEBUG: Error - EM Parity tests failed");
                return FALSE;
        }
        
-       size_t size = removeParity(DemodBuffer, idx + EM_PREAMBLE_LEN, 9, 0, 44);
-       if (!size) {
-               if (g_debugMode) PrintAndLog("DEBUG: Error - EM Parity not detected");
+       if (!removeParity(DemodBuffer, idx + EM_PREAMBLE_LEN, 9, 0, 44)) {
+               if (g_debugMode) PrintAndLog("DEBUG: Error - EM, failed removing parity");
                return FALSE;
        }
-       // set & copy to output
        setDemodBuf(DemodBuffer, 40, 0);
        *word = bytebits_to_byteLSBF(DemodBuffer, 32);
        return TRUE;
        if (!downloadSamplesEM())
                return -1;
 
-       //todo: check response for 00001010 then write data for write confirmation!
-       
+
        //attempt demod:
        //need 0 bits demoded (after preamble) to verify write cmd
        uint32_t dummy = 0;