Replacing the EGR valve could explain the stalling and the P0401, and also why the SES light came on so soon before, (EGR is one of the first monitors to complete). If the problem continues, cleaning the EGR passages in the cylinder heads and intake manifold along with the installation of an EGR gasket with a screen built into it, may be necessary to asure a long term repair.