Move common code to a separate method