modify insert_at_index to fix the bug and make the code clearer