Add exit conditions
Refactor out waitAndListen and create GlobalCounter