use fold instead of recursion