factored out some common code