fixed addElem algorithm