Some refactoring and better waiting paradigm for reduced cpu consumption