moves chapter generation into a separate function