Bug fix with the resolverange function. That function (rather annoyingly) takes a number as well as a table. Due to how Lua handles arguments resolverange(1, 2) is effectively resolverange(1), which will always return 1. The correct syntax is resolverange( {1, 2} ) which passes a table.
Why oh why it does that we may never know. But I was calling it the wrong way originally.
The commenting and reorganizing IS the major change. Just to make this crystal clear:
The point of these updates is to MAKE the above code crystal clear :). If there's a section you think could be easier to understand or you don't like the organization speak up. If you have your own suggestions you can include them as well.