refactoring fold.c