fix waitgroup logic

@wvanbergen @manygrams

Not sure whether this fixes @manygrams's bug, but I noticed this could be done more safely. Thoughts?