Deinit function improved