From here to the else statement is duplicated below and should be collapsed.