Fixed differentiation and added tests for it.