A model of the formation of spiral galaxies