Added error handling and output