Added proper error handling.