Fix bug with insert and add a test case for it