You load storage[0] here and in on line 220.

Move lines 222..224 up and use them when computing d.