refactor derive code; add simple gradient example