diff --git a/pd/src/g_editor.c b/pd/src/g_editor.c
index 7862a94c58bac562abaab6f8329a097dcad79d0e..c294e0287079bd4392d03994c59fdacc918ffb08 100644
--- a/pd/src/g_editor.c
+++ b/pd/src/g_editor.c
@@ -6385,11 +6385,17 @@ void canvas_connect(t_canvas *x, t_floatarg fwhoout, t_floatarg foutno,
         /* if object creation failed, make dummy inlets or outlets
         as needed */ 
     if (pd_class(&src->g_pd) == text_class && objsrc->te_type == T_OBJECT)
+    {
         while (outno >= obj_noutlets(objsrc))
             outlet_new(objsrc, 0);
+        //fprintf(stderr,"canvas_connect got fake outlets\n");
+    }
     if (pd_class(&sink->g_pd) == text_class && objsink->te_type == T_OBJECT)
+    {
         while (inno >= obj_ninlets(objsink))
             inlet_new(objsink, &objsink->ob_pd, 0, 0);
+        //fprintf(stderr,"canvas_connect got fake inlets\n");
+    }
 
     if (!canvas_isconnected(x, objsrc, outno, objsink, inno))
     {
diff --git a/pd/src/g_text.c b/pd/src/g_text.c
index d34b534e3edd111408b6c2f9859e8b846de880b1..81ff821d98105ccd3314358b78840daa235839c8 100644
--- a/pd/src/g_text.c
+++ b/pd/src/g_text.c
@@ -181,8 +181,8 @@ static void canvas_objtext(t_glist *gl, int xpix, int ypix,
     if (!x)
     {
         /* LATER make the color reflect this */
-        //fprintf(stderr,"creating blank object\n");
         x = (t_text *)pd_new(text_class);
+        //fprintf(stderr,"creating blank object %lx\n", x);
     }
     /* special case: an object, like preset_hub, hides its arguments
        beyond the first n, so we modify its binbuf here */
@@ -1407,6 +1407,15 @@ static void text_getrect(t_gobj *z, t_glist *glist,
         int no = obj_noutlets(ob);
         int ni = obj_ninlets(ob);
 
+        /*
+        // debug chunk
+        char *bufdbg;
+        int bufsizedbg;
+        if (y) rtext_gettext(y, &bufdbg, &bufsizedbg);
+        if (!strncmp(bufdbg, "gate", 4) || strlen(bufdbg) == 0)
+            fprintf(stderr,"text_getrect nlets %d %d <%s>\n", ni, no, ( y ? bufdbg : "null" ));
+        */
+
         int m = ( ni > no ? ni : no);
         //let's see if the object has more nlets than
         //its text width and resize them accordingly
@@ -1996,6 +2005,8 @@ void glist_drawiofor_withtag(t_glist *glist, t_object *ob, int firsttime,
 void text_drawborder(t_text *x, t_glist *glist,
     char *tag, int width2, int height2, int firsttime)
 {
+    //fprintf(stderr,"text_drawborder\n");
+
     t_object *ob;
     int x1, y1, x2, y2;
 
@@ -2205,7 +2216,7 @@ void text_drawborder_withtag(t_text *x, t_glist *glist,
                      x2, y2,  x1, y2,  x1, y1, 
                     tag, tag);
     }
-        /* for comments, just draw a bar on RHS if unlocked; when a visible
+        /* for comments, draw a dotted box; when a visible
         canvas is unlocked we have to call this anew on all comments, and when
         locked we erase them all via the annoying "commentbar" tag. */
     else if (x->te_type == T_TEXT && glist->gl_edit)
@@ -2277,6 +2288,23 @@ static int compare_subpatch_selectors(t_atom *a, t_atom *b)
         return 0;
 }
 
+void text_checkvalidwidth(t_glist *glist)
+{
+    // readjust border in case the new object is invalid and it has more connections
+    // than what the default width allows (this typically happens when there is a valid
+    // object that has been replaced by an invalid one and during recreation the new
+    // object has 0 inlets and outlets and is therefore unaware of its possibly greater
+    // width)
+    t_gobj *yg = glist->gl_list;
+    while (yg->g_next)
+        yg = yg->g_next;
+    t_text *newest_t = (t_text *)yg;
+    //fprintf(stderr, "newest object text class is %lx\n", newest_t);
+    t_rtext *yn = glist_findrtext(glist, newest_t);
+    if (yn && pd_class(&newest_t->te_pd) == text_class && newest_t->te_type != T_TEXT)
+        text_drawborder(newest_t, glist, rtext_gettag(yn), rtext_width(yn), rtext_height(yn), 0);
+}
+
     /* change text; if T_OBJECT, remake it. */
 
 void text_setto(t_text *x, t_glist *glist, char *buf, int bufsize, int pos)
@@ -2354,6 +2382,7 @@ void text_setto(t_text *x, t_glist *glist, char *buf, int bufsize, int pos)
                 canvas_restoreconnections(glist_getcanvas(glist));
                 //canvas_apply_restore_original_position(glist_getcanvas(glist),
                 //    pos);
+                text_checkvalidwidth(glist);
             }
             else
             {
diff --git a/pd/src/g_undo.c b/pd/src/g_undo.c
index 6356b7841140be164d23cdb98f18fb70b4c799ea..b0fb5b7126ecfd043625012ea5b2327620915c45 100644
--- a/pd/src/g_undo.c
+++ b/pd/src/g_undo.c
@@ -10,6 +10,8 @@ int we_are_undoing = 0;
 
 extern const char *canvas_undo_name;
 extern void glob_preset_node_list_seek_hub(void);
+extern void glob_preset_node_list_check_loc_and_update(void);
+extern void text_checkvalidwidth(t_glist *glist);
 
 t_undo_action *canvas_undo_init(t_canvas *x)
 {
@@ -94,6 +96,7 @@ void canvas_undo_undo(t_canvas *x)
         {
             sys_vgui("pdtk_undomenu .x%lx %s %s\n",
                 x, undo_action, redo_action);
+            text_checkvalidwidth(x);
             canvas_getscroll(x);
         }
         canvas_dirty(x, 1);
@@ -139,6 +142,7 @@ void canvas_undo_redo(t_canvas *x)
         {
             sys_vgui("pdtk_undomenu .x%lx %s %s\n",
                 x, undo_action, redo_action);
+            text_checkvalidwidth(x);
             canvas_getscroll(x);
         }
         canvas_dirty(x, 1);