fixed bug with calculate grad