big refactor of Element insertion, fix firefox issue