Problem with strings,delay and serial

Ok so im trying to get my arduino mega 2560 based alarm system to work so that when its activated and I call to it from my phone it deactivates the alarm. Im using Sim900 with arduino to receive calls. Im using this code to check if there is call coming when its in activate state.

else if(Serial3.available()){
       while(tarkistus == 0){
      talletus = Serial3.read();
      kirjaimeksi = talletus;
      
      Sarja_data += kirjaimeksi;
      Sarja_data.trim();
      delay(1);
      if(Sarja_data.startsWith("RING")){tarkistus = 1;}
      
      
     }
      delay(10); 
      Serial3.println("ATH1");
      do_something = 2;
    }

This works great if its like this. After this part it goes to check number function which does this

int check_number(){
  Sarja_data = "";
  int dump;
  Serial3.println("AT+CPBS=\x22MC\x22");
  delay(10);
  while(Serial3.available()){ dump = Serial3.read();}
  delay(10);
  Serial3.println("AT+CPBR=1");
  delay(10);
  if(Serial3.available()){
     while(Serial3.available()){
      talletus = Serial3.read();
      kirjaimeksi = talletus;
      Sarja_data += kirjaimeksi;


      delay(1);
     }

     delay(10);
     Sarja_data.trim();
     Serial.println(numero);
            Serial.println(Sarja_data);
     if(Sarja_data.substring(8,21) == numero){return 1;}
     else{
     return 0;
     }
  }
  
}

you might notice there is 2 serial.println:s which are just there to tell me if the code is working or not. In this case it does. These strings are normally printed to serial and everything works.

The problem is that my IR-sensor even in its lowest sensing state picks up something when I call the arduino. I tried to tackle this by adding the first code part also to that ir sensing part with delay of 5 seconds so that the sim900 would have time to send something thru serial and notify arduino that something is calling it and that IR sensor is just acting up. So I did this

    else if(digitalRead(39) == HIGH)
    {
       do_something = 1; //jostain syystä IR-sensori havaitsee jotain kun laitteelle soitetaan.
       
      delay(5000);  // tämä delay mahdollistaa puhelimella hälytyksen deaktivoimisen.
     if(Serial3.available()){
       while(tarkistus == 0){
      talletus = Serial3.read();
      kirjaimeksi = talletus;
      
      Sarja_data += kirjaimeksi;
      Sarja_data.trim();
      delay(1);
      if(Sarja_data.startsWith("RING")){tarkistus = 1;}
      
      
     }
      delay(10); 
      Serial3.println("ATH1");
      do_something = 2;
    }
     
    }
  }

With that delay added it waits for 5 seconds and after that it checks if there is something from sim or not. If there is it goes and checks the number and if its me calling it should just deactivate the alarm. What it does now is that the delay somehow brokes the arduino sim900 connection because now when the program asks the sim for missed calls it just returns ok and not the line of who called last. What im doing wrong here can anybody help me?

Plz if I were unclear on something and you want more info ask away.