Tidied up the indexing code quite a bit, changed scope of writer openage - that now needs closing