race conditions fixes

This pull requests fixes race conditions in tests and listener (I've recently created issues in the main repository).
This line isn't directly addressing either of the two issues you mentioned, but I think it makes sense.