Error handling and added an error log