factored out duplicated code