Refactored the terminal initialization code. Also, we will now properly shut down the terminal at the end.