Fix attach internal node function.

The old internal node function was breaking because it declared **arr and **old and
dereferenced them before it allocated space. Working with this there were a lot more
little problems and it was written kind of confusing so I've tried to simplify it a
while keeping the same functionality. There was also a memory leak caused by not
releasing the internal node's children when it was released.