I made a changed version of the program, it uses robot.inventorySize() at the begining and use this number. It might fix your ploblem. http://pastebin.com/iZTNfd8b
Dig program coded for 16 slot robots, that's why it searches first 16 slots. You can change this number in the code. It could be programmed with one extra function using robot.inventorySize() to get real inventory size of the robot.