Subroutinize the main parts of the auto-alignment routine