Program now terminates correctly! Also, added plot averages