fixed gradient calculation issue