Added function to find an optimal h.