use defer on resetting terminal to make sure we get it back