diff --git a/libs/spandsp/src/fax.c b/libs/spandsp/src/fax.c
index 3a08d49218..90409da7a4 100644
--- a/libs/spandsp/src/fax.c
+++ b/libs/spandsp/src/fax.c
@@ -163,7 +163,8 @@ SPAN_DECLARE_NONSTD(int) fax_rx(fax_state_t *s, int16_t *amp, int len)
 #endif
     for (i = 0;  i < len;  i++)
         amp[i] = dc_restore(&s->modems.dc_restore, amp[i]);
-    s->modems.rx_handler(s->modems.rx_user_data, amp, len);
+    if (s->modems.rx_handler)
+        s->modems.rx_handler(s->modems.rx_user_data, amp, len);
     t30_timer_update(&s->t30, len);
     return 0;
 }
@@ -197,26 +198,6 @@ SPAN_DECLARE_NONSTD(int) fax_rx_fillin(fax_state_t *s, int len)
 }
 /*- End of function --------------------------------------------------------*/
 
-static int set_next_tx_type(fax_state_t *s)
-{
-    fax_modems_state_t *t;
-
-    t = &s->modems;
-    if (t->next_tx_handler)
-    {
-        fax_modems_set_tx_handler(t, t->next_tx_handler, t->next_tx_user_data);
-        t->next_tx_handler = NULL;
-        return 0;
-    }
-    /* If there is nothing else to change to, so use zero length silence */
-    silence_gen_alter(&t->silence_gen, 0);
-    fax_modems_set_tx_handler(t, (span_tx_handler_t) &silence_gen, &t->silence_gen);
-    fax_modems_set_next_tx_handler(t, (span_tx_handler_t) NULL, NULL);
-    t->transmit = false;
-    return -1;
-}
-/*- End of function --------------------------------------------------------*/
-
 SPAN_DECLARE_NONSTD(int) fax_tx(fax_state_t *s, int16_t *amp, int max_len)
 {
     int len;
@@ -226,41 +207,30 @@ SPAN_DECLARE_NONSTD(int) fax_tx(fax_state_t *s, int16_t *amp, int max_len)
     required_len = max_len;
 #endif
     len = 0;
-    if (s->modems.transmit)
+    while (s->modems.transmit  &&  (len += s->modems.tx_handler(s->modems.tx_user_data, &amp[len], max_len - len)) < max_len)
     {
-        while ((len += s->modems.tx_handler(s->modems.tx_user_data, amp + len, max_len - len)) < max_len)
-        {
-            /* Allow for a change of tx handler within a block */
-            if (set_next_tx_type(s)  &&  s->modems.current_tx_type != T30_MODEM_NONE  &&  s->modems.current_tx_type != T30_MODEM_DONE)
-                t30_front_end_status(&s->t30, T30_FRONT_END_SEND_STEP_COMPLETE);
-            if (!s->modems.transmit)
-            {
-                if (s->modems.transmit_on_idle)
-                {
-                    /* Pad to the requested length with silence */
-                    memset(amp + len, 0, (max_len - len)*sizeof(int16_t));
-                    len = max_len;
-                }
-                break;
-            }
-        }
+        /* Allow for a change of tx handler within a block */
+        if (fax_modems_set_next_tx_type(&s->modems)  &&  s->modems.current_tx_type != T30_MODEM_NONE  &&  s->modems.current_tx_type != T30_MODEM_DONE)
+            t30_front_end_status(&s->t30, T30_FRONT_END_SEND_STEP_COMPLETE);
+        /*endif*/
     }
-    else
+    /*endwhile*/
+    if (s->modems.transmit_on_idle)
     {
-        if (s->modems.transmit_on_idle)
-        {
-            /* Pad to the requested length with silence */
-            memset(amp, 0, max_len*sizeof(int16_t));
-            len = max_len;
-        }
+        /* Pad to the requested length with silence */
+        memset(&amp[len], 0, (max_len - len)*sizeof(int16_t));
+        len = max_len;
     }
+    /*endif*/
 #if defined(LOG_FAX_AUDIO)
     if (s->modems.audio_tx_log >= 0)
     {
         if (len < required_len)
-            memset(amp + len, 0, (required_len - len)*sizeof(int16_t));
+            memset(&amp[len], 0, (required_len - len)*sizeof(int16_t));
+        /*endif*/
         write(s->modems.audio_tx_log, amp, required_len*sizeof(int16_t));
     }
+    /*endif*/
 #endif
     return len;
 }
diff --git a/libs/spandsp/src/fax_modems.c b/libs/spandsp/src/fax_modems.c
index f3c175eb8c..e298f4d70a 100644
--- a/libs/spandsp/src/fax_modems.c
+++ b/libs/spandsp/src/fax_modems.c
@@ -533,6 +533,23 @@ SPAN_DECLARE(void) fax_modems_set_next_tx_handler(fax_modems_state_t *s, span_tx
 }
 /*- End of function --------------------------------------------------------*/
 
+SPAN_DECLARE(int) fax_modems_set_next_tx_type(fax_modems_state_t *s)
+{
+    if (s->next_tx_handler)
+    {
+        fax_modems_set_tx_handler(s, s->next_tx_handler, s->next_tx_user_data);
+        fax_modems_set_next_tx_handler(s, (span_tx_handler_t) NULL, NULL);
+        return 0;
+    }
+    /* There is nothing else to change to, so use zero length silence */
+    silence_gen_alter(&s->silence_gen, 0);
+    fax_modems_set_tx_handler(s, (span_tx_handler_t) &silence_gen, &s->silence_gen);
+    fax_modems_set_next_tx_handler(s, (span_tx_handler_t) NULL, NULL);
+    s->transmit = false;
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
 SPAN_DECLARE(void) fax_modems_set_tep_mode(fax_modems_state_t *s, int use_tep)
 {
     s->use_tep = use_tep;
diff --git a/libs/spandsp/src/t31.c b/libs/spandsp/src/t31.c
index 79ba8f5286..76aa7e56ab 100644
--- a/libs/spandsp/src/t31.c
+++ b/libs/spandsp/src/t31.c
@@ -2830,7 +2830,8 @@ SPAN_DECLARE_NONSTD(int) t31_rx(t31_state_t *s, int16_t amp[], int len)
     }
     /*endif*/
 
-    s->audio.modems.rx_handler(s->audio.modems.rx_user_data, amp, len);
+    if (s->audio.modems.rx_handler)
+        s->audio.modems.rx_handler(s->audio.modems.rx_user_data, amp, len);
     return 0;
 }
 /*- End of function --------------------------------------------------------*/
@@ -2861,23 +2862,6 @@ SPAN_DECLARE_NONSTD(int) t31_rx_fillin(t31_state_t *s, int len)
 }
 /*- End of function --------------------------------------------------------*/
 
-static int set_next_tx_type(t31_state_t *s)
-{
-    if (s->audio.next_tx_handler)
-    {
-        fax_modems_set_tx_handler(&s->audio.modems, s->audio.next_tx_handler, s->audio.next_tx_user_data);
-        fax_modems_set_next_tx_handler(&s->audio.modems, (span_tx_handler_t) NULL, NULL);
-        return 0;
-    }
-    /*endif*/
-    /* There is nothing else to change to, so use zero length silence */
-    silence_gen_alter(&s->audio.modems.silence_gen, 0);
-    fax_modems_set_tx_handler(&s->audio.modems, (span_tx_handler_t) &silence_gen, &s->audio.modems.silence_gen);
-    fax_modems_set_next_tx_handler(&s->audio.modems, (span_tx_handler_t) NULL, NULL);
-    return -1;
-}
-/*- End of function --------------------------------------------------------*/
-
 SPAN_DECLARE_NONSTD(int) t31_tx(t31_state_t *s, int16_t amp[], int max_len)
 {
     int len;
@@ -2888,8 +2872,8 @@ SPAN_DECLARE_NONSTD(int) t31_tx(t31_state_t *s, int16_t amp[], int max_len)
         if ((len = s->audio.modems.tx_handler(s->audio.modems.tx_user_data, amp, max_len)) < max_len)
         {
             /* Allow for one change of tx handler within a block */
-            set_next_tx_type(s);
-            if ((len += s->audio.modems.tx_handler(s->audio.modems.tx_user_data, amp + len, max_len - len)) < max_len)
+            fax_modems_set_next_tx_type(&s->audio.modems);
+            if ((len += s->audio.modems.tx_handler(s->audio.modems.tx_user_data, &amp[len], max_len - len)) < max_len)
                 front_end_status(s, T30_FRONT_END_SEND_STEP_COMPLETE);
             /*endif*/
         }
@@ -2899,7 +2883,7 @@ SPAN_DECLARE_NONSTD(int) t31_tx(t31_state_t *s, int16_t amp[], int max_len)
     if (s->audio.modems.transmit_on_idle)
     {
         /* Pad to the requested length with silence */
-        memset(amp + len, 0, (max_len - len)*sizeof(int16_t));
+        memset(&amp[len], 0, (max_len - len)*sizeof(int16_t));
         len = max_len;
     }
     /*endif*/
diff --git a/libs/spandsp/src/t38_gateway.c b/libs/spandsp/src/t38_gateway.c
index 77225cc63f..c41df87c75 100644
--- a/libs/spandsp/src/t38_gateway.c
+++ b/libs/spandsp/src/t38_gateway.c
@@ -2061,7 +2061,8 @@ SPAN_DECLARE_NONSTD(int) t38_gateway_rx(t38_gateway_state_t *s, int16_t amp[], i
     for (i = 0;  i < len;  i++)
         amp[i] = dc_restore(&s->audio.modems.dc_restore, amp[i]);
     /*endfor*/
-    s->audio.modems.rx_handler(s->audio.modems.rx_user_data, amp, len);
+    if (s->audio.modems.rx_handler)
+        s->audio.modems.rx_handler(s->audio.modems.rx_user_data, amp, len);
     return 0;
 }
 /*- End of function --------------------------------------------------------*/
@@ -2102,12 +2103,13 @@ SPAN_DECLARE_NONSTD(int) t38_gateway_tx(t38_gateway_state_t *s, int16_t amp[], i
 
     required_len = max_len;
 #endif
+    len = 0;
     if ((len = s->audio.modems.tx_handler(s->audio.modems.tx_user_data, amp, max_len)) < max_len)
     {
         if (set_next_tx_type(s))
         {
             /* Give the new handler a chance to file the remaining buffer space */
-            len += s->audio.modems.tx_handler(s->audio.modems.tx_user_data, amp + len, max_len - len);
+            len += s->audio.modems.tx_handler(s->audio.modems.tx_user_data, &amp[len], max_len - len);
             if (len < max_len)
             {
                 silence_gen_set(&s->audio.modems.silence_gen, 0);
@@ -2121,7 +2123,7 @@ SPAN_DECLARE_NONSTD(int) t38_gateway_tx(t38_gateway_state_t *s, int16_t amp[], i
     if (s->audio.modems.transmit_on_idle)
     {
         /* Pad to the requested length with silence */
-        memset(amp + len, 0, (max_len - len)*sizeof(int16_t));
+        memset(&amp[len], 0, (max_len - len)*sizeof(int16_t));
         len = max_len;
     }
     /*endif*/
@@ -2129,7 +2131,7 @@ SPAN_DECLARE_NONSTD(int) t38_gateway_tx(t38_gateway_state_t *s, int16_t amp[], i
     if (s->audio.modems.audio_tx_log >= 0)
     {
         if (len < required_len)
-            memset(amp + len, 0, (required_len - len)*sizeof(int16_t));
+            memset(&amp[len], 0, (required_len - len)*sizeof(int16_t));
         /*endif*/
         write(s->audio.modems.audio_tx_log, amp, required_len*sizeof(int16_t));
     }