I'm kinda uncomfortable with your global usage (both here and for mutex) it would be better to work around needing them.