A two-stage scenario for the formation of the Earth's mantle and core