Refactor terminal.d