added static boolean flag to control thread