Got rid of recursion in folds and scans.

fixes #32