refactor terminal code and add some header files