Array not working as intended?

    Serial.flush();

Block until all pending serial data has been sent. Since you haven't sent any, please explain how this is useful.

    for (int i = 0; i < Serial.available(); i++)
    {
      val[i] = Serial.read();
    }

Just how big is val?

    if(val[1] != 'a')

That would have thrown an array out of bounds exception, if the Arduino had exceptions.