trying to make folding a loop instead of recursive