https://stackoverflow.com/questions/1630320/what-is-the-pythonic-way-to-detect-the-last-element-in-a-python-for-loop

I'll review the rest of the PR later on. I've been quite busy with Heartbleed in the past few hours :)