Tweaked callbacks to prevent race condition (https://github.com/libevent/libevent/issues/104)