divide_recipe <- function(recipe) {

  beta_recipe <- list(Description = recipe$Description, 
                      background = NULL,
                      tree = NULL,
                      balls = NULL,
                      snow = NULL,
                      star = NULL,
                      fileout = paste0(dirname(recipe$fileout),
                                       "/santas_workshop/",
                                       basename(recipe$fileout)))

  all_recipes <- rep(list(beta_recipe), length(recipe$background))
  for (col in 1:length(recipe$background)) {
    all_recipes[[col]]$background <- recipe$background[[col]]
  }
  
  for (col in 1:length(recipe$tree)) {
    for (reci in 1:length(all_recipes)) {
      all_recipes[[reci]]$tree <- recipe$tree[[col]]
    }
    if (col == 1) {
      recipes <- all_recipes
    } else {
      recipes <- append(recipes, all_recipes)
    }
  }
  all_recipes <- recipes

  recipes <- list()
  for (col in 1:length(recipe$balls)) {
    for (reci in 1:length(all_recipes)) {
      all_recipes[[reci]]$balls <- recipe$balls[[col]]
    }
    if (col == 1) {
      recipes <- all_recipes
    } else {
      recipes <- append(recipes, all_recipes)
    }
  }
  all_recipes <- recipes

  recipes <- list()
  for (col in 1:length(recipe$snow)) {
    for (reci in 1:length(all_recipes)) {
      all_recipes[[reci]]$snow <- recipe$snow[[col]]
    }
    if (col == 1) {
      recipes <- all_recipes
    } else {
      recipes <- append(recipes, all_recipes)
    }
  }
  all_recipes <- recipes


  recipes <- list()
  for (col in 1:length(recipe$star)) {
    for (reci in 1:length(all_recipes)) {
      all_recipes[[reci]]$star <- recipe$star[[col]]
    }
    if (col == 1) {
      recipes <- all_recipes
    } else {
      recipes <- append(recipes, all_recipes)
    }
  }
  all_recipes <- recipes

  # Save all recipes in separate YAML files
  for (reci in 1:length(all_recipes)) {
    if (reci < 10) {
      recipe_number <- paste0("0", reci)
    } else {
      recipe_number <- reci
    }
    all_recipes[[reci]]$recipe_number <- recipe_number
    if (!dir.exists(paste0(dirname(recipe$fileout), "/santas_workshop/"))) {
      dir.create(paste0(dirname(recipe$fileout), "/santas_workshop/"),
                 recursive = TRUE)
    }
    write_yaml(all_recipes[[reci]], 
               paste0(dirname(recipe$fileout), "/santas_workshop/atomic_recipe_",
                      recipe_number, ".yml"))
  }
  return(list(n_atomic_recipes = length(all_recipes)))
}
