add function that remove unnecessary nodes