added memory management
added test for null pointer before deleting