Why is this code being duplicated? Can this be moved to a method?