refactored and fixed non-tty session issues