Improve memory management
Fix memory leak