Rewritten terminal logic, adds basic cmd redirection.
Fixes #399.