Updated source code to calc gradient fields