This function is a bit long and hard to read / follow. Is it possible to split it in multiple smaller functions (e.g. _find_image, _register_group, ...)?