Format the code via pycharm's auto format. Add profilehooks dep.

profilehooks is a slick lib that let's us easily profile code with
just a decorator.
Rename variables so code is readable. Move local vars close to usage.
Simplify conditionals. Remove unnecessary reassignment of var.

The last part, i -= 1, had no effect whatsoever.
Replace unnecessary array iteration with a loop.