Factor out functions for build_image