seperated header & middleware generation into two different functions