tweak to gradient code