Change a few global variables to constants

This reduces the number of global variables and fixes a potential bug with the `dtr` global variable, which is not a constant.

I think this is ready to merge ... I'll restructure the code to get rid of all global variables in a future PR.