replace recursive call with fold - it should remove increase of stack, but not sure how to test