Move duplicated logic into method.