As the universe expanded and cooled further, gravitational attraction caused matter to coalesce into larger structures. Regions with slightly higher densities attracted more matter, leading to the formation of galaxies and stars. The first generation of stars, known as Population III stars, were massive and short-lived, contributing to the chemical enrichment of the universe.