Rewritten the terminal application mode. Made many other minor changes as well.