repairs derivative function and little refactor
repairs derivative function and little refactor