A number of constants are now defined at the top of the file, instead of hardcoded within individual functions. Some small changes have been made elsewhere.