diff --git a/ChangeLog b/ChangeLog
index ec32a0fc5a769b9c1ed19a3ad71a7f70c4259f3a..2afeea2d8a7ae7acfff9259a06b242160f74d909 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -9,6 +9,10 @@ CVS code
 	  DOS/Mac format, at least one line has already been converted, 
 	  so setting NOCONVERT (which is supposed to signal that none 
 	  of the file should be converted) makes no sense. (DLR)
+- nano.c:
+  justify_format()
+	- Fix ugly behavior when wrapping spaces at the end of long
+	  words (David Benbennick).
 - nanorc.5:
 	- Fix formatting error and update copyright year (Jordi).
 	- Several enhancements (David Benbennick).
diff --git a/nano.c b/nano.c
index f23f21ff0929cb6aa0cc55b72a4b839e23ca74d2..db83a997392a50db04568de0e4ecf0826490dcd2 100644
--- a/nano.c
+++ b/nano.c
@@ -2047,7 +2047,7 @@ int justify_format(int changes_allowed, filestruct *line, size_t skip)
 
     back = line->data + skip;
     front = back;
-    for (; *front; front++) {
+    for (front = back; ; front++) {
 	int remove_space = 0;
 	    /* Do we want to remove this space? */
 
@@ -2057,11 +2057,11 @@ int justify_format(int changes_allowed, filestruct *line, size_t skip)
 	    *front = ' ';
 	}
 	/* these tests are safe since line->data + skip is not a space */
-	if (*front == ' ' && *(front - 1) == ' ') {
+	if ((*front == '\0' || *front == ' ') && *(front - 1) == ' ') {
 	    const char *bob = front - 2;
 
 	    remove_space = 1;
-	    for (bob = front - 2; bob >= line->data + skip; bob--) {
+	    for (bob = back - 2; bob >= line->data + skip; bob--) {
 		if (strchr(punct, *bob) != NULL) {
 		    remove_space = 0;
 		    break;
@@ -2080,21 +2080,18 @@ int justify_format(int changes_allowed, filestruct *line, size_t skip)
 	    if (mark_beginbuf == line && back - line->data < mark_beginx)
 		mark_beginx--;
 #endif
+	    if (*front == '\0')
+		*(back - 1) = '\0';
 	} else {
 	    *back = *front;
 	    back++;
 	}
+	if (*front == '\0')
+	    break;
     }
 
-    /* Remove spaces from the end of the line, except maintain 1 after a
-     * sentence punctuation. */
-    while (line->data < back && *(back - 1) == ' ')
-	back--;
-    if (line->data < back && *back == ' ' &&
-	strchr(punct, *(back - 1)) != NULL)
-	back++;
-    if (!changes_allowed && back != front)
-	return 1;
+    back--;
+    assert(*back == '\0');
 
     /* This assert merely documents a fact about the loop above. */
     assert(changes_allowed != 0 || back == front);
@@ -2253,7 +2250,7 @@ int break_line(const char *line, int goal, int force)
 	/* No space found short enough. */
 	if (force)
 	    for(; *line != '\0'; line++, cur_loc++)
-		if (*line == ' ' && *(line + 1) != ' ')
+		if (*line == ' ' && *(line + 1) != ' ' && *(line + 1) != '\0')
 		    return cur_loc;
 	return -1;
     }