Debug functions need to be at top of file to be seen

Fix typo in debug message
Rename a confusing variable

This also exposed a potential bug that is not yet tested for where the offset
variable was shadowed by a local redeclaration.