G-Code buffer help

I am using a Duet Wifi on a custom built printer and I'm having trouble getting Gcode to execute as intended. I am using a fan channel to control a motor via a relay. This motor performs a mid print process that I cant really discuss but the motor needs to be ON while certain moves are being executed, then turned OFF. What is actually happening is that the motor turns on earlier than intended and cuts off earlier as well. I believe this has something to do with the way the buffer code is executed. I have tried adding pauses before and after the M106 commands and this helped a little but it is still not working properly. Is there a way to get the M106 command to execute in the correct sequence?