Added missing range check for insert position