Improve game tree database interaction.

Significant improvement to the way the game tree interacts with the database. I.e., it no longer makes like 100 calls every single time.

NOTE: This pull request's branch seems to include the commits that make up my other open pull request. I don't think it should matter too much, but if they're both accepted for merging then you should only merge this one in - and then close the other pull request.