Indexing refactor

Finally! Right?

Don't get too excited yet: this is passing the tests locally but I can't actually use it yet due to a problem with the initial handshake. I'll give the nod when I'm happy with it, but I felt the PR should be alive.