Hoist var definitions, add comments and TODOs, minor copyedits, minor code simplification