Heavily reorganise front end code.