made some refactoring to remove while(true)