Move duplicated code into method.