Clean up imports.
Continue implementing open logic. Stdio are properly sync now.

Lots of extra cleanup in here as old code starts to fall off.
Fix TTY calculation, at least for terminals we consider stdio.