Added condition for terminating console. Fixed  #517