bit of refactoring and add TTY checking