Refactor and add mathcheck

Pattern %x can be used to match hexadecimal numbers http://www.lua.org/pil/20.2.html .