refactor duplicate folding code into one method

Break out common code into its own method.