fixed bug causing access to negative array index