Fixed joints handling

Now pointers to joints are removed correctly when needed.