Rewrite a function to use a fold