new differentiation method
removed unnecessary brackets in gradient expr
made vars local
shuffled statements, rewrote gradient init in model function
moved static statements to let block