Generalize folding functions