Moved code into sensible subroutines.