Refactored monadic folds with (>>=) instead of do.