Correcting bug in derivative calculation (not yet corrected)