Refactoring resource/memory management (removing __del__ usage)