Finally correct implementation of insert.