datastore: refactor transaction & datastore.

Hello again!

I rearranged a few things, hopefully for the better. I'll leave comments. :)
Please send code reorgs in a different PR, I can't see the diff.