Factored out some duplicated code