Implement insert and fix some bugs.