this logic is duplicated twice. can it be turned into a util function?