Fix bugs and refactor dependencies search in remove