Added smart pointers and exception handling.