Refactor image processing functionality