check for index before assigning