collapse nest of funstions