Refactored code for simplification, prepared for image expansion before registration.