Refactor code for image processing