Fix bug in index function

I've added a failing test to show what appears to be a bug in indexing (although please let me know if I've misunderstood the expected functionality).

This can be fixed similarly to how it's done in #5 however it'd be worth keeping the refactoring here so we get the tests for `createIndex`.