added validation for image, and code refactoring for better maintenance