Fixed issues with memory deallocation using smart pointers