fixed more bugs. 'i' was re-used in an inner scope. Close files after use, which fixed segfaults.
caught a couple more indexing bugs. full audit necessary