Stripping out some of my debug logging and using the each_with_index idiom to tidy it up a bit.
Refactoring and trying to make it a bit cleaner and clearer.