The law is a essentially the enforceable moral code of the state that enforces it. Most criminal laws were created to penalise acts that are considered morally reprehensible. I wouldn’t say the law is an afterthought around morality but a reflection of the morality of the state. The laws are largely written by the capitalistic class and are a reflection of what they consider right and wrong.