clean up of main file, subfunctions for doing the job for disaply and saving