I would prefer this to be a named function, rather than anonymous, to clean up the main block here and reduce indentation a bit.