Index: commands.c
===================================================================
--- commands.c	(revision 8ee712e00f681e910612f6f4eb7eabf7799ed2f7)
+++ commands.c	(revision 9eb38bb4825e0c7f73087fbd9f851aa0305ccedd)
@@ -2772,5 +2772,9 @@
   pw = owl_global_get_popwin(&g);
   owl_global_set_popwin(&g, NULL);
-  owl_viewwin_cleanup(vw);
+  /* Kind of a hack, but you can only have one active viewwin right
+   * now anyway. */
+  if (vw == owl_global_get_viewwin(&g))
+    owl_global_set_viewwin(&g, NULL);
+  owl_viewwin_delete(vw);
   owl_popwin_delete(pw);
   owl_global_pop_context(&g);
Index: functions.c
===================================================================
--- functions.c	(revision 03ca0054628460c6cc077c799c50e2dac636549a)
+++ functions.c	(revision 9eb38bb4825e0c7f73087fbd9f851aa0305ccedd)
@@ -1237,7 +1237,5 @@
   owl_viewwin *v;
 
-  v=owl_global_get_viewwin(&g);
-
-  if (owl_global_get_popwin(&g)) {
+  if (owl_global_get_popwin(&g) || owl_global_get_viewwin(&g)) {
     owl_function_error("Popwin already in use.");
     return;
@@ -1246,6 +1244,9 @@
   owl_global_set_popwin(&g, pw);
   owl_popwin_up(pw);
+
+  v = owl_viewwin_new_text(owl_popwin_get_content(pw), text);
+  owl_global_set_viewwin(&g, v);
+
   owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless", NULL);
-  owl_viewwin_init_text(v, owl_popwin_get_content(pw), text);
 }
 
@@ -1255,7 +1256,5 @@
   owl_viewwin *v;
 
-  v=owl_global_get_viewwin(&g);
-
-  if (owl_global_get_popwin(&g)) {
+  if (owl_global_get_popwin(&g) || owl_global_get_viewwin(&g)) {
     owl_function_error("Popwin already in use.");
     return;
@@ -1264,6 +1263,9 @@
   owl_global_set_popwin(&g, pw);
   owl_popwin_up(pw);
+
+  v = owl_viewwin_new_fmtext(owl_popwin_get_content(pw), fm);
+  owl_global_set_viewwin(&g, v);
+
   owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless", NULL);
-  owl_viewwin_init_fmtext(v, owl_popwin_get_content(pw), fm);
 }
 
Index: global.c
===================================================================
--- global.c	(revision 03ca0054628460c6cc077c799c50e2dac636549a)
+++ global.c	(revision 9eb38bb4825e0c7f73087fbd9f851aa0305ccedd)
@@ -49,4 +49,5 @@
 
   g->pw = NULL;
+  g->vw = NULL;
   g->tw = NULL;
 
@@ -492,5 +493,9 @@
 
 owl_viewwin *owl_global_get_viewwin(owl_global *g) {
-  return(&(g->vw));
+  return g->vw;
+}
+
+void owl_global_set_viewwin(owl_global *g, owl_viewwin *vw) {
+  g->vw = vw;
 }
 
Index: owl.h
===================================================================
--- owl.h	(revision 03ca0054628460c6cc077c799c50e2dac636549a)
+++ owl.h	(revision 9eb38bb4825e0c7f73087fbd9f851aa0305ccedd)
@@ -596,5 +596,5 @@
   void *buffercbdata;
   owl_editwin *tw;
-  owl_viewwin vw;
+  owl_viewwin *vw;
   void *perl;
   int debug;
Index: popexec.c
===================================================================
--- popexec.c	(revision 03ca0054628460c6cc077c799c50e2dac636549a)
+++ popexec.c	(revision 9eb38bb4825e0c7f73087fbd9f851aa0305ccedd)
@@ -17,5 +17,5 @@
   pid_t pid;
 
-  if (owl_global_get_popwin(&g)) {
+  if (owl_global_get_popwin(&g) || owl_global_get_viewwin(&g)) {
     owl_function_error("Popwin already in use.");
     return NULL;
@@ -28,12 +28,12 @@
   pe->refcount=0;
 
-  pe->vwin=v=owl_global_get_viewwin(&g);
   pw = owl_popwin_new();
   owl_global_set_popwin(&g, pw);
   owl_popwin_up(pw);
+  pe->vwin = v = owl_viewwin_new_text(owl_popwin_get_content(pw), "");
+  owl_global_set_viewwin(&g, v);
+  owl_viewwin_set_onclose_hook(v, owl_popexec_viewwin_onclose, pe);
 
   owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless", NULL);
-  owl_viewwin_init_text(v, owl_popwin_get_content(pw), "");
-  owl_viewwin_set_onclose_hook(v, owl_popexec_viewwin_onclose, pe);
   pe->refcount++;
 
Index: viewwin.c
===================================================================
--- viewwin.c	(revision 61c1f1934d06ed9432e8476870774a778b74c1ab)
+++ viewwin.c	(revision 9eb38bb4825e0c7f73087fbd9f851aa0305ccedd)
@@ -7,9 +7,11 @@
 static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w);
 
-/* initialize the viewwin e.  'win' is an already initialzed curses
- * window that will be used by viewwin
+/* Create a viewwin.  'win' is an already initialized owl_window that
+ * will be used by the viewwin
  */
-void owl_viewwin_init_text(owl_viewwin *v, owl_window *win, const char *text)
+owl_viewwin *owl_viewwin_new_text(owl_window *win, const char *text)
 {
+  owl_viewwin *v = owl_malloc(sizeof(owl_viewwin));
+  memset(v, 0, sizeof(*v));
   owl_fmtext_init_null(&(v->fmtext));
   if (text) {
@@ -25,4 +27,5 @@
 
   owl_viewwin_set_window(v, win);
+  return v;
 }
 
@@ -42,10 +45,12 @@
 }
 
-/* initialize the viewwin e.  'win' is an already initialzed curses
- * window that will be used by viewwin
+/* Create a viewwin.  'win' is an already initialized owl_window that
+ * will be used by the viewwin
  */
-void owl_viewwin_init_fmtext(owl_viewwin *v, owl_window *win, const owl_fmtext *fmtext)
+owl_viewwin *owl_viewwin_new_fmtext(owl_window *win, const owl_fmtext *fmtext)
 {
   char *text;
+  owl_viewwin *v = owl_malloc(sizeof(owl_viewwin));
+  memset(v, 0, sizeof(*v));
 
   owl_fmtext_copy(&(v->fmtext), fmtext);
@@ -60,12 +65,9 @@
 
   owl_viewwin_set_window(v, win);
+  return v;
 }
 
 static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w)
 {
-  if (v->window) {
-    g_signal_handler_disconnect(v->window, v->sig_redraw_id);
-    g_object_unref(v->window);
-  }
   v->window = w;
   if (w) {
@@ -181,7 +183,6 @@
 }
 
-void owl_viewwin_cleanup(owl_viewwin *v)
+void owl_viewwin_delete(owl_viewwin *v)
 {
-  owl_viewwin_set_window(v, NULL);
   if (v->onclose_hook) {
     v->onclose_hook(v, v->onclose_hook_data);
@@ -189,4 +190,10 @@
     v->onclose_hook_data = NULL;
   }
+  if (v->window) {
+    g_signal_handler_disconnect(v->window, v->sig_redraw_id);
+    g_object_unref(v->window);
+    v->window = NULL;
+  }
   owl_fmtext_cleanup(&(v->fmtext));
+  owl_free(v);
 }
