Refactor using hash instead of two arrays, and a few other improvements. Runtime on my 174 MB sample file went from 3.5 minutes to 4 seconds!