Change for loop to retrieve all messages and use a temporary variable.