Added debug information and improved error handling