diff --git a/mi/mieq.c b/mi/mieq.c
index 5bd87eb..8929010 100644
--- a/mi/mieq.c
+++ b/mi/mieq.c
@@ -108,9 +108,13 @@ mieqEnqueue(DeviceIntPtr pDev, xEvent *e)
     HWEventQueueType       oldtail = miEventQueue.tail, newtail;
     int                    isMotion = 0;
     deviceValuator         *v = (deviceValuator *) e;
-    EventPtr               laste = &miEventQueue.events[oldtail - 1];
-    deviceKeyButtonPointer *lastkbp = (deviceKeyButtonPointer *)
-                                      &laste->event[0];
+    EventPtr               laste;
+    deviceKeyButtonPointer *lastkbp;
+
+    if (oldtail == 0)
+        laste = &miEventQueue.events[QUEUE_SIZE - 1];
+    else
+        laste = &miEventQueue.events[oldtail - 1];
 
     if (e->u.u.type == MotionNotify)
         isMotion = inputInfo.pointer->id;
@@ -124,6 +128,7 @@ mieqEnqueue(DeviceIntPtr pDev, xEvent *e)
             ErrorF("mieqEnqueue: more than six valuator events; dropping.\n");
             return;
         }
+        lastkbp = (deviceKeyButtonPointer *) &laste->event[0]
         if (oldtail == miEventQueue.head ||
             !(lastkbp->type == DeviceMotionNotify ||
               lastkbp->type == DeviceButtonPress ||
