seperated calculation of gradient b. Added function to calculate h