Refactored folding operation