Fix some tests
Modify residuals
Modify calculate_gradients