factor out common code