Does this function definition need to be inside this closure? Could we take it out and simplify?