Deallocates all main variables, then calls the cleanup routines of all relevant subroutines to do the same thing.