Moved material transition code to a separate subroutine and tweaked it to check again for transitions after chaning the material number