Correct cases where the X-axis of the bone (in pose space) aligns with the pose-space Y or Z-axis. In these cases the decomposition of the matrix fails, and a negative scale of the X-axis turns into a 180° rotation around the Y-axis. An extra -1 scale to the X and Z axes of the resulting matrix seems to fix things.