factored out common code;