cleanup and add support for Terminal emulator