The engine seems fine most of the time but it idles rough and occasionally jumps between 500 and 1200 rpm when I shift to drive or reverse. When I throttle and it hits 4000 rpm, the 'Check Engine Light' starts flashing. Can you give me advice as to what to do to fix all of this?
You may have a couple of separate issues:
The flashing Check Engine Light under heavy load is most likely an ignition system misfire. This could be something like a coil pack or worn spark plugs. These engines are known for going through coil packs so that would be my first guess.
The first step is to retrieve the diagnostic trouble codes for your Check Engine Light which serve as a starting point for any further diagnosis. I suggest you have a trained professional, such as one from YourMechanic, diagnose your vehicle firsthand.