Factor out function call fold logic