check index before assigning, addresses #1547