Refactor and add mathcheck

It does, but at a cost to readability and modifiability, as well as a function call, and LuaJIT is pretty effective at dealing with variables efficiently; I'd prefer to keep this one as-is.