Serial.available acting funny

Your code doesn't compile:

sketch_oct22b.cpp: In function 'void loop()':
sketch_oct22b:54: error: expected `}' at end of input

Please note that, at present, the String library has bugs as discussed here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).