fix derivative calculations, and add (limited) test cases