All the guys have given good advice. Connectors, I spray the pins with contact cleaner and then scrub them the best I can, with one of those tooth brushes for between your teeth. More often than not, the motor runs better.
Vacuum, either the vacuum connections or the intake manifold (check the bolts, I've found mine loose before). You end up with some cylinders slightly rich and some slightly lean. Which can't be good for a smooth running motor. How is the idle RPM?
Distributor, if you can move the shaft sideways, more than very little, like a thousandths or 2, it´s gonna run bad and often the rotor will strike the cap high voltage studs. Up and down play on the shaft, doesn't seem to make much difference.
I've had an O2 sensor make mine buck at around 2000 RPM and then the replacement made it idle like chit. Took two tries to find one that worked well. The one that idled bad was OEM.
Some sensors make it run bad from the get go, some when it gets up to operating temp. Helps narrow it down some.
Check under the ignition coil, separate the coil from the ignition control module and clean the contacts. I went to remove a spark plug cable from my cap (a month old) and the whole stud came out of the cap. All sort of strange things can go wrong. Spark plug cable are always suspect, even new ones, I've had new ones, separate the end from the wire inside the boot (hard to see).
A stuck IAC will make it run rough till you get the RPM´s up, it does more than just idle.
Do you have a bottom end knock of any sort? My motor was running well, but did the shimmy shake at idle. A new set of connecting rod bearings sure did smooth things out. I get the feeling the rough idle was messing with my CPS sensor and the inputs to the ICU, can´t prove it, but the motor smoothed out a lot more than a marginal set of connecting rod bearings can account for. I also scrubbed the engine temp. sender, knock sensor and O2 sensor plugs while I was under the motor. That could also be part of the improvement.
At 337,000 yours almost has to need connecting rod and main bearings. I've seen numerous 4.0's that had enough crank gyration from worn main bearings to wear out a rear main seal twice a year. Which pretty much has to mess up the CPS pulses, somewhat.