Added error checking to image upload functionality