Move some more code around
Add toFoldM and fromFoldM