This bit is duplicated, can it be split out into one function?