How to prevent the dreaded Modafinil headaches

I need to be taking a nearly 6 hour exam tomorrow and in similar situations have settled on using Moda to keep myself alert and focused, and its worked fantastic. I take it very sporadically as I do not want to disrupt my sleep schedule and I've had nothing but success so far UNTIL recently. The last two times I've taken it I have gotten headaches that would not go away with anything, ibuprofen, acetaminophen, magneisum, you name it. The only thing I could do was take melatonin and sleep it off. The second time was worse than the first.

Has anyone figured a way to prevent/mitigate this? For reference, my dosage was about the same the two times I got headaches (about 120-140 mg). I drink a ton of caffeine and do my best to avoid it while I take it, but i still have coffee in the morning to prevent myself from getting withdrawal headaches. I make sure I sleep very well the night before taking it. Any advice would help, thank you!