Factored out some frequently used code