added check for already referenced nodes, updated js