is a possible source for that sort of problem. Strings (capital S) don't work well in the small memory of the Arduino. They can eventually corrupt the memory.
Use strings (small s) which are arrays of char terminated with a 0. That is what is used in Serial Input Basics
If that is not the problem may I suggest that you write an explanation of how your program works - there is a lot of code.