Added error handling and fixed folder creation for caching images.