improvements to handling which message should be marked as sent and delivered, instead of just using the first message