Added a check to avoid an out of range execption