Added terminal detection