cleanup and extra algos/data structures
added foldable instance