race conditions fixes

This pull requests fixes race conditions in tests and listener (I've recently created issues in the main repository).