Started tidying up main.c, no significant functional changes.