refactor and move to tty.js