Help Regarding Flipping Character Right-Side Up Using FromToRotation

Hi, I wrote some code that aligns the player’s rotation to the normal of the surface they’re walking on, and if they’re not on the ground, it flips them so they’re the right way up.

My issue is that the code for when they’re not on the ground is not working as I would like. I’d like to rotate the player right-side-up whilst keeping them pointed in the direction they’re moving, however when they’re at a greater than 90 degree angle, the code flips them the right way up but points them in the opposite direction they were facing.

Here’s the code that handles the flipping. The else part is the part I’m having trouble with:

    Quaternion AlignToSurface()
    {
        Quaternion retVal = new Quaternion();

        if(groundChecker.IsGrounded)
        {
            Quaternion alignedRotation = Quaternion.FromToRotation(transform.up, groundChecker.Normal) * transform.rotation; 
            retVal = Quaternion.Lerp(transform.rotation, alignedRotation, 10f * Time.deltaTime);
        }
        else
        {
            Quaternion alignedRotation = Quaternion.FromToRotation(transform.up, Vector3.up) * transform.rotation;
            retVal = Quaternion.Lerp(transform.rotation, alignedRotation, 10f * Time.deltaTime);
        }

        return retVal;
    }

And here’s the code that I’m using to apply the rotation:

rigidbody.MoveRotation(AlignToSurface().normalized);

This is the only place where the rotation is being modified and I haven’t been able to figure it out so any help would be appreciated.