Slightly rework logic to get rid of superfluous inner function def.