implement fold and add testing code