This is the second, slightly different, version of this logic.  Can we extract it to a shared location?  Thanks.