Converted msgstats macros & Co. to functions and added range checks