Remove duplicate code and fix variable delete.