Could just be a lack of certain minerals/electrolytes, high salt intake, high stress hormones, or high blood sugar. It may take time to fix if you're eating better now and your diet was shit for a long time, especially if you have absorption issues.
I wouldn't necessarily stress about sodium/potassium because most people ingest plenty of both, but a good magnesium supplement is one I would consider taking daily (because it's stripped from soil now) and adjust the dose if you experience loose stools.