added code to stop and terminate instances