Refactored function for collecting the image