Index: commands.c
===================================================================
--- commands.c	(revision 1a0874fe5f57a0c90fee1c151de027a85c198032)
+++ commands.c	(revision 8ae2de9d9b1137206d709b38c6bcb828269af527)
@@ -2662,5 +2662,5 @@
   owl_editwin_fullclear(e);
   owl_global_set_needrefresh(&g);
-  wnoutrefresh(owl_editwin_get_curswin(e));
+  update_panels();
   owl_global_set_typwin_inactive(&g);
   owl_editwin_new_style(e, OWL_EDITWIN_STYLE_ONELINE, NULL);
@@ -2730,5 +2730,5 @@
   owl_free(cmd);
 
-  wnoutrefresh(owl_editwin_get_curswin(e));
+  update_panels();
   owl_global_set_needrefresh(&g);
 
@@ -2747,5 +2747,5 @@
   owl_global_pop_context(&g);
   owl_editwin_fullclear(e);
-  wnoutrefresh(owl_editwin_get_curswin(e));
+  update_panels();
   owl_global_set_needrefresh(&g);
 }
@@ -2765,5 +2765,5 @@
   owl_global_pop_context(&g);
   owl_global_set_needrefresh(&g);
-  wnoutrefresh(owl_editwin_get_curswin(e));
+  update_panels();
 }
 
Index: editwin.c
===================================================================
--- editwin.c	(revision d41f7736c88bcaf7e39b9379753dfa744539cff1)
+++ editwin.c	(revision 8ae2de9d9b1137206d709b38c6bcb828269af527)
@@ -567,5 +567,5 @@
   e->cursorx = x;
 
-  wnoutrefresh(e->curswin);
+  update_panels();
   if (update == 1)
     doupdate();
Index: functions.c
===================================================================
--- functions.c	(revision 91634ec817b2d3dfda0c429002c8450e4f2404d5)
+++ functions.c	(revision 8ae2de9d9b1137206d709b38c6bcb828269af527)
@@ -218,5 +218,5 @@
     owl_popwin_refresh(owl_global_get_popwin(&g));
   }
-  wnoutrefresh(owl_global_get_curs_recwin(&g));
+  update_panels();
   owl_global_set_needrefresh(&g);
 }
@@ -1252,4 +1252,5 @@
 void owl_function_set_cursor(WINDOW *win)
 {
+  /* Be careful that this window is actually empty, otherwise panels get confused */
   wnoutrefresh(win);
 }
@@ -1265,8 +1266,5 @@
       redrawwin(owl_global_get_curs_msgwin(&g));
 
-  wnoutrefresh(owl_global_get_curs_recwin(&g));
-  wnoutrefresh(owl_global_get_curs_sepwin(&g));
-  wnoutrefresh(owl_global_get_curs_typwin(&g));
-  wnoutrefresh(owl_global_get_curs_msgwin(&g));
+  update_panels();
 
   if (owl_popwin_is_active(owl_global_get_popwin(&g))) {
@@ -3404,5 +3402,5 @@
   owl_function_debugmsg("makemsg: %s", buff);
   waddstr(owl_global_get_curs_msgwin(&g), buff);  
-  wnoutrefresh(owl_global_get_curs_msgwin(&g));
+  update_panels();
   owl_global_set_needrefresh(&g);
   va_end(ap);
Index: global.c
===================================================================
--- global.c	(revision 516c27e292e8048a6dd07f5f3733f8c30eff92d4)
+++ global.c	(revision 8ae2de9d9b1137206d709b38c6bcb828269af527)
@@ -126,4 +126,21 @@
 }
 
+/* If *pan does not exist, we create a new panel, otherwise we replace the
+   window in *pan with win.
+
+   libpanel PANEL objects cannot exist without owner a valid window. This
+   maintains the invariant for _owl_global_setup_windows. */
+void _owl_panel_set_window(PANEL **pan, WINDOW *win)
+{
+  WINDOW *oldwin;
+  if (*pan) {
+    oldwin = panel_window(*pan);
+    replace_panel(*pan, win);
+    delwin(oldwin);
+  } else {
+    *pan = new_panel(win);
+  }
+}
+
 void _owl_global_setup_windows(owl_global *g) {
   int cols, typwin_lines;
@@ -142,5 +159,5 @@
 
   /* create the new windows */
-  g->recwin=newwin(g->recwinlines, cols, 0, 0);
+  g->recwin = newwin(g->recwinlines, cols, 0, 0);
   if (g->recwin==NULL) {
     owl_function_debugmsg("_owl_global_setup_windows: newwin returned NULL\n");
@@ -148,8 +165,12 @@
     exit(50);
   }
+  _owl_panel_set_window(&g->recpan, g->recwin);
       
-  g->sepwin=newwin(1, cols, g->recwinlines, 0);
-  g->msgwin=newwin(1, cols, g->recwinlines+1, 0);
-  g->typwin=newwin(typwin_lines, cols, g->recwinlines+2, 0);
+  g->sepwin = newwin(1, cols, g->recwinlines, 0);
+  _owl_panel_set_window(&g->seppan, g->sepwin);
+  g->msgwin = newwin(1, cols, g->recwinlines+1, 0);
+  _owl_panel_set_window(&g->msgpan, g->msgwin);
+  g->typwin = newwin(typwin_lines, cols, g->recwinlines+2, 0);
+  _owl_panel_set_window(&g->typpan, g->typwin);
 
   owl_editwin_set_curswin(g->tw, g->typwin, typwin_lines, g->cols);
@@ -469,14 +490,7 @@
   if (!g->resizepending) return;
 
-  /* delete the current windows */
-  delwin(g->recwin);
-  delwin(g->sepwin);
-  delwin(g->msgwin);
-  delwin(g->typwin);
   if (!isendwin()) {
     endwin();
   }
-
-  refresh();
 
   /* get the new size */
Index: mainwin.c
===================================================================
--- mainwin.c	(revision 129e60962226d924d19aa15f7f74b866733b8e47)
+++ mainwin.c	(revision 8ae2de9d9b1137206d709b38c6bcb828269af527)
@@ -43,5 +43,5 @@
     mw->curtruncated=0;
     mw->lastdisplayed=-1;
-    wnoutrefresh(recwin);
+    update_panels();
     owl_global_set_needrefresh(&g);
     return;
@@ -141,5 +141,5 @@
   mw->lastdisplayed=i-1;
 
-  wnoutrefresh(recwin);
+  update_panels();
   owl_global_set_needrefresh(&g);
 }
Index: owl.c
===================================================================
--- owl.c	(revision 91634ec817b2d3dfda0c429002c8450e4f2404d5)
+++ owl.c	(revision 8ae2de9d9b1137206d709b38c6bcb828269af527)
@@ -599,5 +599,5 @@
   owl_function_source(NULL);
 
-  wrefresh(sepwin);
+  update_panels();
 
   /* Set the default style */
@@ -655,4 +655,6 @@
     /* update the terminal if we need to */
     if (owl_global_is_needrefresh(&g)) {
+      /* ensure that panels are clean, so that set_cursor doesn't break things */
+      update_panels();
       /* leave the cursor in the appropriate window */
       if (owl_global_is_typwin_active(&g)) {
Index: owl.h
===================================================================
--- owl.h	(revision be1ae91c351d5b24497f838a8abc03130a7cb936)
+++ owl.h	(revision 8ae2de9d9b1137206d709b38c6bcb828269af527)
@@ -394,5 +394,7 @@
 typedef struct _owl_popwin {
   WINDOW *borderwin;
+  PANEL *borderpanel;
   WINDOW *popwin;
+  PANEL *poppanel;
   int lines;
   int cols;
@@ -556,4 +558,5 @@
   owl_messagelist msglist;
   WINDOW *recwin, *sepwin, *msgwin, *typwin;
+  PANEL *recpan, *seppan, *msgpan, *typpan;
   int needrefresh;
   int rightshift;
Index: popwin.c
===================================================================
--- popwin.c	(revision 8240bce075da77f2d4bd353e9b0407e35207d738)
+++ popwin.c	(revision 8ae2de9d9b1137206d709b38c6bcb828269af527)
@@ -25,5 +25,7 @@
 
   pw->borderwin=newwin(pw->lines, pw->cols, startline, startcol);
+  pw->borderpanel = new_panel(pw->borderwin);
   pw->popwin=newwin(pw->lines-2, pw->cols-2, startline+1, startcol+1);
+  pw->poppanel = new_panel(pw->popwin);
   pw->needsfirstrefresh=1;
   
@@ -48,6 +50,5 @@
   }
     
-  wnoutrefresh(pw->popwin);
-  wnoutrefresh(pw->borderwin);
+  update_panels();
   owl_global_set_needrefresh(&g);
   pw->active=1;
@@ -57,4 +58,6 @@
 int owl_popwin_close(owl_popwin *pw)
 {
+  del_panel(pw->poppanel);
+  del_panel(pw->borderpanel);
   delwin(pw->popwin);
   delwin(pw->borderwin);
@@ -75,9 +78,5 @@
 int owl_popwin_refresh(const owl_popwin *pw)
 {
-  touchwin(pw->borderwin);
-  touchwin(pw->popwin);
-
-  wnoutrefresh(pw->borderwin);
-  wnoutrefresh(pw->popwin);
+  update_panels();
   owl_global_set_needrefresh(&g);
   return(0);
Index: util.c
===================================================================
--- util.c	(revision a61daaecdc8be025b0df7ab3dc8e5fbe7c858a83)
+++ util.c	(revision 8ae2de9d9b1137206d709b38c6bcb828269af527)
@@ -120,5 +120,5 @@
   wattroff(sepwin, A_BOLD);
   wattroff(sepwin, A_REVERSE);
-  wnoutrefresh(sepwin);
+  update_panels();
 }
 
Index: viewwin.c
===================================================================
--- viewwin.c	(revision 3e552686ce4ec66095c90a50587d0caf65cbd2d8)
+++ viewwin.c	(revision 8ae2de9d9b1137206d709b38c6bcb828269af527)
@@ -89,5 +89,5 @@
   }
   wattroff(v->curswin, A_REVERSE);
-  wnoutrefresh(v->curswin);
+  update_panels();
 
   if (update==1) {
