modified to handle terminal issues