flag variable was not working - fixed it