Add a terminal module

And refactor to use it.