completed transcribing main function except for plotting