Refactored with smart pointers