Spline twists when y value of tangent reaches zero


I’m trying to generate splines at runtime. Everything works great, execpt when the tangent values approaches zero, the spline mesh always twists. I have tried setting the upvector at each point to the normal of the spline but it does not help the case. Is it something to do with the spline mesh tangent values? Any help would be great. Thanks!