This subroutine needs two more what-if loops.