fixed gradient (off by 2) and made tol relative