In my opinion, the main purpose behind law and punishment is deterrence from immoral actions. Punishment for its own sake is in no way good, it's a necessary evil to deter immoral people from committing immoral actions. If there were no immoral people, there would be no one to deter and thus no use for law.
Sure, internal morality can get twisted, but only when it's based in instinct (self preservation, subordination to authority, etc.), and that's what I mean when I say pseudo-morality. Rational moral principles, on the other hand, are relatively reliable, clear and consistent. The main moral frameworks (deontology, consequentialism, virtue ethics) agree on most significant scenarios, the differences are in the specificities.
The scapegoat phenomenon you mention is a great example of instinct (the ingroup-outgroup instinct), being taken advantage of for political manipulation. Education should include teaching people to recognize these instincts and when they're leading them astray in rational thought.
I think simply achieving material flourishing isn't enough to make people moral, mainly because there are other instincts at play. For example the desire for status and power. This is what drives people to immorality even during material satisfaction. I also believe it is the main driving force behind capitalism.