Fixed a few race conditions

I think this one particular race condition was responsible for the bot
randomly disconnecting.
Also, fixing the race condition was hacked in, I might find a cleaner
way of fixing it in the future.