added new,safer version
revised deallocation