Updated with latest revisions suggested by @jcorbin. It's now over the 500 lines mark (558) so I'll come back and rip things out later with the next set of edits.