Refactor: improved image_pre_handler code