factored out some code