add comments, move stuff around, fix derivative