Refactoring to allow better user experience in handling database failures.
Also prevent users from uploading wrong type of images.