fixes #1 using wait-and-notify

This may not be as well-tested as you'd like since it's more of a backport from my working-copy. The working copy is a complete mess since I used it to try and understand the plugin and is littered with unrelated comments and changes. 

I also tried to mirror your coding style, hope that worked out