add
if(pos < 0) pos = 0;
to avoid array having minus value.