diff --git a/blxdotter.c b/blxdotter.c
index 0e514378124a2333bd245f776417ad4dd172b3c4..3d103717add14fcd47e1c0a11e3aac3574be5ecb 100644
--- a/blxdotter.c
+++ b/blxdotter.c
@@ -459,6 +459,16 @@ void showDotterDialog(GtkWidget *blxWindow, const gboolean bringToFront)
       /* Create the dialog data struct first time round, but re-populate it each time. Create 
        * a destructor function that will free the struct. */
       dialogData = g_malloc(sizeof(DotterDialogData));
+      dialogData->blxWindow = NULL;
+      dialogData->autoButton = NULL;
+      dialogData->manualButton = NULL;
+      dialogData->startEntry = NULL;
+      dialogData->endEntry = NULL;
+      dialogData->zoomEntry = NULL;
+      dialogData->callOnSelf = FALSE;
+      dialogData->hspsOnly = FALSE;
+      dialogData->dotterSSeq = NULL;
+
       g_signal_connect(G_OBJECT(dialog), "destroy", G_CALLBACK(onDestroyDotterDialog), dialogData);
       
       g_signal_connect(dialog, "response", G_CALLBACK(onResponseDotterDialog), dialogData);
@@ -532,6 +542,13 @@ void showDotterDialog(GtkWidget *blxWindow, const gboolean bringToFront)
   dialogData->zoomEntry = zoomEntry;
   dialogData->callOnSelf = FALSE;
   dialogData->hspsOnly = FALSE;
+  
+  if (dialogData->dotterSSeq)
+    {
+      g_free(dialogData->dotterSSeq);
+      dialogData->dotterSSeq = NULL;
+    }
+  
   dialogData->dotterSSeq = getDotterSSeq(blxWindow, NULL);
   
   /* There is an issue if the user selects a different sequence while the dotter dialog
diff --git a/blxwindow.c b/blxwindow.c
index 4b4f3e4ac321b984310da355e0fb3b6c98e4496e..fbda7c0f2f4dbdc9ea02b48149bd8ad8a88a6d40 100755
--- a/blxwindow.c
+++ b/blxwindow.c
@@ -5199,8 +5199,11 @@ static void calcID(MSP *msp, BlxViewContext *bc)
                       int sIdx = s_start, qIdx = q_start ;
                       while (((sForward && sIdx < sRangeMax) || (!sForward && sIdx >= sRangeMin - 1)) && qIdx < qLen)
                         {
-                          if (toupper(matchSeq[sIdx]) == toupper(refSeqSegment[qIdx]))
-                            numMatchingChars++ ;
+                          /* Check that qIdx is not less that 0, which could happen if we have somehow got duff data. */
+                          if (qIdx >= 0 && toupper(matchSeq[sIdx]) == toupper(refSeqSegment[qIdx]))
+                            {
+                              numMatchingChars++ ;
+                            }
                           
                           /* Move to the next base. The refSeqSegment is always forward, but we might have to
                            * traverse the s sequence in reverse. */