Cleaned up and commented the code for the terminal control.