merging in the useful parts of the bot3 code