Isn't this code a duplicate of a similar existing function? If so, can it be refactored to a common place?