diff --git a/qrenc.c b/qrenc.c
index c09c4ab..17a7bf3 100644
--- a/qrenc.c
+++ b/qrenc.c
@@ -724,7 +724,7 @@ static int writeXPM(const QRcode *qrcode, const char *outfile)
 	}
 
 	for (y = 0; y < realmargin; y++) {
-		fprintf(fp, "\"%s\"%s\n", row, y < (size - 1) ? "," : "};");
+		fprintf(fp, "\"%s\"%s\n", row, y < (realmargin - 1) ? "," : "};");
 	}
 
 	free(row);
