removed dead space and used DRY for inserting new elements into tree