This function is pretty much identical to generatePage with the exception that you're constructing slightly different objects. How about you write a function that has this same functionality, but instead of explicitly creating "new Category" or "new Page.withCategories" said function takes as parameters functions that do this specialized object construction, so you can share code?