Add check on array key to prevent "undefined index" error