Flagging at 4000 rpms
If yours is an electronic throttle, this sounds like early failure of it's wiring harness. The wiring insulation in the harness to the actuators is faulty and turns to crumbs from the heat. When thing jiggle around--wiring starts to leak electrically. the computer gets faulty signals and doesn't know what to do open the throttle, close it, shut off the fuel ?
I hope this is not your problem, but do not be surprised if yours is an electronic throttle engine. $1000+