Added proper error handling