Add folding utilities.
a little modified.