Improve terminal detection