Moved folding logic around