Refactor and add mathcheck

Nit: localize variable.