moved kderivative to its own function