refactor element insertion and iron out a few edge cases