updated logic. added check for stopping