Improved error handling and URL parsing