Fixed bugs for not handling folding correctly.