Erasing unnecessary elements, setting references