added checking to terminate-instance