factoring out imperative code with Reduce's and Lapply's