factored out some functions into a helper