Index: fmtext.c
===================================================================
--- fmtext.c	(revision 0331c8f1f28f0990034921fbff0515ebca04d676)
+++ fmtext.c	(revision 801b7ac63b962640debbcfd422cb9a60da5fea31)
@@ -239,5 +239,4 @@
       fg = f->fgcolorbuff[position];
       bg = f->bgcolorbuff[position];
-      owl_function_debugmsg("waddstr: fg(%i) bg(%i).", fg, bg);
 
       pair = owl_fmtext_get_colorpair(fg, bg);
@@ -696,5 +695,5 @@
 {
   owl_colorpair_mgr *cpmgr;
-  short pair, i, default_bg;
+  short pair, default_bg;
 
 #ifdef HAVE_USE_DEFAULT_COLORS
Index: functions.c
===================================================================
--- functions.c	(revision 29ebceaa347bc2c0947c5886c226a75ba048c97e)
+++ functions.c	(revision 801b7ac63b962640debbcfd422cb9a60da5fea31)
@@ -1273,5 +1273,5 @@
 int owl_function_calculate_topmsg_normal(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
 {
-  int savey, j, i, foo, y;
+  int savey, i, foo, y;
 
   if (curmsg<0) return(topmsg);
@@ -1282,8 +1282,14 @@
   }
 
-  /* Find number of lines from top to bottom of curmsg (store in savey) */
-  savey=0;
-  for (i=topmsg; i<=curmsg; i++) {
-    savey+=owl_message_get_numlines(owl_view_get_element(v, i));
+  /* If curmsg is so far past topmsg that there are more messages than
+     lines, skip the line counting that follows because we're
+     certainly off screen.  */
+  savey=curmsg-topmsg;
+  if (savey <= recwinlines) {
+    /* Find number of lines from top to bottom of curmsg (store in savey) */
+    savey = 0;
+    for (i=topmsg; i<=curmsg; i++) {
+      savey+=owl_message_get_numlines(owl_view_get_element(v, i));
+    }
   }
 
@@ -1292,5 +1298,5 @@
   if (savey > recwinlines) {
     topmsg=curmsg;
-    savey=owl_message_get_numlines(owl_view_get_element(v, i));
+    savey=owl_message_get_numlines(owl_view_get_element(v, curmsg));
     direction=OWL_DIRECTION_UPWARDS;
   }
@@ -1300,10 +1306,10 @@
     if (savey < (recwinlines / 4)) {
       y=0;
-      for (j=curmsg; j>=0; j--) {
-	foo=owl_message_get_numlines(owl_view_get_element(v, j));
+      for (i=curmsg; i>=0; i--) {
+	foo=owl_message_get_numlines(owl_view_get_element(v, i));
 	/* will we run the curmsg off the screen? */
 	if ((foo+y) >= recwinlines) {
-	  j++;
-	  if (j>curmsg) j=curmsg;
+	  i++;
+	  if (i>curmsg) i=curmsg;
 	  break;
 	}
@@ -1312,6 +1318,6 @@
 	if (y > (recwinlines / 2)) break;
       }
-      if (j<0) j=0;
-      return(j);
+      if (i<0) i=0;
+      return(i);
     }
   }
@@ -1322,12 +1328,12 @@
       y=0;
       /* count lines from the top until we can save 1/2 the screen size */
-      for (j=topmsg; j<curmsg; j++) {
-	y+=owl_message_get_numlines(owl_view_get_element(v, j));
+      for (i=topmsg; i<curmsg; i++) {
+	y+=owl_message_get_numlines(owl_view_get_element(v, i));
 	if (y > (recwinlines / 2)) break;
       }
-      if (j==curmsg) {
-	j--;
+      if (i==curmsg) {
+	i--;
       }
-      return(j+1);
+      return(i+1);
     }
   }
Index: view.c
===================================================================
--- view.c	(revision f1e629de3f6889c21f5f9dd7225e11c4fe9408b4)
+++ view.c	(revision 801b7ac63b962640debbcfd422cb9a60da5fea31)
@@ -97,15 +97,29 @@
 int owl_view_get_nearest_to_msgid(owl_view *v, int targetid)
 {
-  int i, bestdist=-1, bestpos=0, curid, curdist;
+  int first, last, mid = 0, max, bestdist, curid = 0;
 
-  for (i=0; i<owl_view_get_size(v); i++) {
-    curid = owl_message_get_id(owl_view_get_element(v, i));
-    curdist = abs(targetid-curid);
-    if (bestdist<0 || curdist<bestdist) {
-      bestdist = curdist;
-      bestpos = i;
+  first = 0;
+  last = max = owl_view_get_size(v) - 1;
+  while (first <= last) {
+    mid = (first + last) / 2;
+    curid = owl_message_get_id(owl_view_get_element(v, mid));
+    if (curid == targetid) {
+      return(mid);
+    } else if (curid < targetid) {
+      first = mid + 1;
+    } else {
+      last = mid - 1;
     }
   }
-  return (bestpos);
+  bestdist = abs(targetid-curid);
+  if (curid < targetid && mid+1 < max) {
+    curid = owl_message_get_id(owl_view_get_element(v, mid+1));
+    mid = (bestdist < abs(targetid-curid)) ? mid : mid+1;
+  }
+  else if (curid > targetid && mid-1 >= 0) {
+    curid = owl_message_get_id(owl_view_get_element(v, mid-1));
+    mid = (bestdist < abs(targetid-curid)) ? mid : mid-1;
+  }
+  return mid;
 }
 
